import verification, {VerificationType} from '$utils/verification';
export default <LibMixins> {

    data(){
        return {
            vValue:'',
            status_fetch_key:'search'
        }
    },

    watch:{
      value:{
          handler:function () {
              return this.setValue(this.value);
          },
          immediate:true
      },
      vValue:function () {
          return this.updateValue(this.vValue);
      },
    },

    methods:{

        setValue(value){
            if (this.vValue !== value) {
                this.vValue = value;
            }
        },

        updateValue(value){
            if (this.value !== value) {
                return this.$emit('update:value',value);
            }
        },

        triggerSearchValue(){

            if (verification.trigger[VerificationType.empty](this.vValue)) {

                if (this.vValue !== this.searchValue ||  this.vValue === this.searchValue && !this.storageData) {
                    this.searchValue = this.vValue;
                    this.storageData = null;
                    this.retrySearchValue();
                } else {
                    return this.triggerSearchSuccess(this.storageData);
                }

            } else {
                return this.$emit('searchEmpty');
            }

        },

        triggerChangeStatus(status,data){

            if (status === this.constStatus.success) {
                this.storageData = data;
                return this.triggerSearchSuccess(data);
            }

        },

        retrySearchValue(){
            this.statusFetch(true,{
                value: this.searchValue
            });
        },

        triggerSearchSuccess(data){
            return this.$emit('searchSuccess',data);
        }

    },

    created(){
        this.searchValue = this.value || '';
    }

}