var t=require("vue");function e(){return(e=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t}).apply(this,arguments)}var i={enableCache:!0,insertOrder:!1,returnRoot:!1},o=function(){function t(t){var e=this;for(var o in this.options=i,this.root={},this.index=0,t)Object.prototype.hasOwnProperty.call(t,o)&&(this.options[o]=t[o]);"function"!=typeof this.options.insert&&(this.options.insert=function(t,i){return e.options.insertOrder&&!(void 0!==(o=i).d&&void 0!==o.o)&&e.index++,t&&t.length?t.push(i):t=[i],t;var o}),"function"!=typeof this.options.sort&&(this.options.insertOrder?this.options.insertOrder&&(this.options.sort=function(){this.sort(function(t,e){return void 0===t.o||void 0===e.o?0:t.o-e.o})}):this.options.sort=function(){this.sort()}),"function"!=typeof this.options.clip&&(this.options.clip=function(t){this.length>t&&this.splice(t,this.length-t)}),"function"!=typeof this.options.copy&&(this.options.copy=function(t){return t.slice(0)}),"function"!=typeof this.options.merge&&(this.options.merge=function(t,i,o){for(var s=0,n=i.length;s<n;s++)t=e.options.insert.call(e,t,i[s]),e.options.sort.call(t,o);return t})}var e=t.prototype;return e._addCacheData=function(t,e){return!(this.root===t&&!this.options.returnRoot||!1===this.options.enableCache||(t.$d||(t.$d={}),t.$d=this.options.insert.call(this,t.$d,e),this.options.sort.call(t.$d),0))},e._addSuffix=function(t,e,i){var o=t.charAt(0),s=t.substring(1)||null,n={$d:{}};s&&(n.$s=s),void 0===i[o]?i[o]=n:void 0===i[o].$d&&(i[o].$d={},s&&void 0===i[o].$s&&(i[o].$s=s)),i[o].$d=this.options.insert.call(this,i[o].$d,e),this.options.sort.call(i[o].$d)},e._moveSuffix=function(t,e,i){var o=t.charAt(0),s=t.substring(1)||null,n={$d:{}};s&&(n.$s=s),void 0===i[o]&&(i[o]=n),i[o].$d=this.options.copy(e)},e._getDataAtNode=function(t,e){var i;return this.options.enableCache?(this.options.sort.call(t.$d,e),i=t.$d):i=this._getSubtree(t,e),this.options.insertOrder&&(i=this._stripInsertOrder(i)),i?this.options.copy(i):void 0},e._stripInsertOrder=function(t){if(void 0!==t){for(var e=[],i=0,o=t.length;i<o;i++)e.push(t[i].d);return e}},e._getSubtree=function(t,e){for(var i,o=[],s=[t];i=s.pop();)for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&("$d"==n?o=this.options.merge.call(this,o,i.$d,e):"$s"!=n&&s.push(i[n]));return o},e.add=function(t,e){if("string"!=typeof t)return!1;1==arguments.length&&(e=t),t=t.toLowerCase();for(var i=this.root,o=0,s=t.length;o<s;o++){var n=t.charAt(o);if(i[n])o==s-1?(this._addCacheData(i,e),this._addCacheData(i[n],e)||this._addSuffix(n,e,i)):(this._addCacheData(i,e),i=i[n]);else{if(i.$s){if(i.$s==t.substring(o)){this._addCacheData(i,e)||(i.$d=this.options.insert.call(this,i.$d,e),this.options.sort.call(i.$d));break}this._moveSuffix(i.$s,i.$d,i),delete i.$s,!1===this.options.enableCache&&delete i.$d}if(!i[n]){this._addSuffix(t.substring(o),e,i),this._addCacheData(i,e);break}this._addCacheData(i,e),o==s-1&&(i[n].$s?(this._moveSuffix(i[n].$s,i[n].$d,i[n]),delete i[n].$s,!1===this.options.enableCache&&delete i[n].$d,this._addSuffix(n,e,i)):this._addCacheData(i[n],e)||this._addSuffix(n,e,i)),i=i[n]}}},e.contains=function(t){if("string"!=typeof t||""==t)return!1;t=t.toLowerCase();for(var e=this.root,i=0,o=t.length;i<o;i++){var s=t.charAt(i);if(!e[s])return!(!e.$s||e.$s!==t.substring(i));e=e[s]}return!(!e.$d||void 0!==e.$s)},e.find=function(t){if("string"==typeof t&&(""!=t||this.options.returnRoot)){t=t.toLowerCase();for(var e=this.root,i=0,o=t.length;i<o;i++){var s=t.charAt(i);if(!e[s])return e.$s&&0==e.$s.indexOf(t.substring(i))?this._getDataAtNode(e,t):void 0;e=e[s]}return this._getDataAtNode(e,t)}},t}();function s(t){return!!t.bind}var n=t.defineComponent({name:"tab-complete-input",emits:["tab-failed","tab-success","tab-ended","selection-changed","update:modelValue"],data:function(){return{trie:new o,position:0,wordPos:0,index:0,words:[],word:"",possible:!1,saved:!1,localValue:this.modelValue,isTypeahead:!1}},render:function(){var i=this,o=[this.tabComplete];return"function"==typeof this.$attrs.onKeydown&&o.push(this.$attrs.onKeydown),Array.isArray(this.$attrs.onKeydown)&&o.push.apply(o,this.$attrs.onKeydown),t.h("input",e({ref:"input"},this.$props,this.$attrs,{value:this.modelValue,"onUpdate:modelValue":function(t){return i.localValue=t},onKeydown:o,onInput:function(t){i.updateValue(t.target.value),i.typeaheadCompletion()}}))},created:function(){s(this.dataSource)||this.setData(this.dataSource)},props:{dataSource:{default:function(){return[]}},format:{type:Function,default:function(t,e,i){return{word:t,prev:e}}},modelValue:{default:""},startCompletionChar:{default:"@"}},watch:{dataSource:function(t){s(t)||this.setData(t)}},methods:{setData:function(t){var e=this;this.trie=new o,t.forEach(function(t){e.trie.add(t)})},getCurrentWord:function(){this.position=this.getCursorPos();var t=this.localValue.slice(0,this.position)+" "+this.localValue.slice(this.position);this.words=t.split(" ");for(var e=0,i=0;i<this.words.length;i++)if((e+=this.words[i].length+1)>=this.position){this.word=this.words[i],this.wordPos=i;break}},getCompletions:function(t){try{var e=this;function i(){e.saved=!0,e.possible=e.trie.find(e.word),e.emitEvents(t)}var o=function(){if(s(e.dataSource)){var t=e.dataSource(e.word,e.wordPos);return Promise.resolve(t).then(function(t){e.setData(t)})}}();return Promise.resolve(o&&o.then?o.then(i):i())}catch(t){return Promise.reject(t)}},selectCompletion:function(e){var i=this;if(this.possible){void 0!==e&&e<this.possible.length&&(this.index=e),this.isTypeahead=!1;var o=this.words,s="";o.length>1&&(s=o[this.wordPos-1]);var n=this.format(this.possible[this.index],s,this.wordPos);o[this.wordPos]=n.word,n.prev&&(o[this.wordPos-1]=n.prev);var r=this.words.slice(0,this.wordPos+1).join(" ").length;this.localValue=o.join(" "),this.localValue=this.localValue.slice(0,r)+this.localValue.slice(r+1),this.updateValue(this.localValue),t.nextTick(function(){return i.selectRange(r,r)}),this.$emit("selection-changed",{completions:this.possible,word:this.word,current:this.index})}},emitEvents:function(t){this.$emit(this.possible?"tab-success":this.saved?"tab-failed":"tab-ended",{original:t,completions:this.possible,word:this.word,current:this.index})},handleTabPressed:function(t){try{var e=this;function i(){e.possible&&(null==t||t.preventDefault(),e.index>=e.possible.length&&(e.index=0),e.selectCompletion())}var o=function(){if(!e.saved)return null==t||t.preventDefault(),Promise.resolve(e.getCompletions(t)).then(function(){});e.index++}();return Promise.resolve(o&&o.then?o.then(i):i())}catch(t){return Promise.reject(t)}},tabComplete:function(t){try{var e,i=this;function o(o){if(e)return o;i.isTypeahead||(i.saved=!1,i.possible=!1,i.index=0,i.emitEvents(t))}if(!t)return Promise.resolve();i.getCurrentWord();var s=function(){if("tab"===t.key||9===t.keyCode)return Promise.resolve(i.handleTabPressed(t)).then(function(){e=1})}();return Promise.resolve(s&&s.then?s.then(o):o(s))}catch(t){return Promise.reject(t)}},typeaheadCompletion:function(){try{var t,e=this;function i(i){if(t)return i;var o=function(){if(e.isTypeahead)return Promise.resolve(e.getCompletions()).then(function(){e.possible||(e.isTypeahead=!1)})}();return o&&o.then?o.then(function(){}):void 0}var o=function(){if(e.word.startsWith(e.startCompletionChar))return e.word=e.word.replace(e.startCompletionChar,""),Promise.resolve(e.getCompletions()).then(function(){e.isTypeahead=!0,t=1})}();return Promise.resolve(o&&o.then?o.then(i):i(o))}catch(t){return Promise.reject(t)}},updateValue:function(t){this.localValue=t,this.$emit("update:modelValue",t)},selectRange:function(t,e){this.$el.focus(),this.$el.setSelectionRange(t,e)},getCursorPos:function(){return this.$el.selectionStart}}});module.exports=n;
//# sourceMappingURL=tab-complete-input.js.map
