import {
    notification
} from 'ant-design-vue';
import {PopupExportComponent} from "$popup/popup-export/const";

export default <Record<string, LibMixinsMethod | any>>{

    $popup:undefined,

    // 打开
    $open(name:PopupExportComponent,option:Record<string, any>){
        return this.$popup && this.$popup.open(name,option);
    },

    // 提示弹窗
    $toast:function (option:PopupToastOptions) {
        return this.$popup && this.$popup.open(PopupExportComponent.toast,this._handleOption(option));
    },

    // 确认窗
    $confirm:function (option:PopupToastOptions) {
        return this.$popup && this.$popup.open(PopupExportComponent.confirm,this._handleOption(option));
    },

    // 选择框
    $select:function (option:PopupSelectOptions){
        return this.$popup && this.$popup.open(PopupExportComponent.select,option);
    },

    // 关闭确认弹窗
    closeConfirm:function (){
        return this.$popup && this.$popup.open(PopupExportComponent.confirm,{title:''});
    },

    // 加载
    $loading:function (option:PopupToastOptions){
        return this.$popup && this.$popup.open(PopupExportComponent.loading,this._handleOption(option));
    },

    // 隐藏
    hideLoading:function () {
        return this.$popup && this.$popup.open(PopupExportComponent.loading,{title:''});
    },

    // 通知框
    $success:function (option:PopupToastOptions) {
        return this._handleNotice('success',option);
    },
    $fail:function (option:PopupToastOptions) {
        return this._handleNotice('error',option);
    },
    $info:function (option:PopupToastOptions) {
        return this._handleNotice('info',option);
    },
    $warning:function (option:PopupToastOptions) {
        return this._handleNotice('warning',option);
    },

    // 处理
    _handleNotice(type,option:PopupToastOptions){
        let resultData:Record<string, any> = {};
        if (typeof option === 'string') {
            resultData={message:option}
        } else {
            resultData= {
                ...option,
                duration: option.duration ? option.duration / 1000 : 4,
                message: option.title
            };
        }

        return  notification[type](resultData);
    },

    // 处理基本的options
    _handleOption(option:PopupToastOptions):Record<string, any>{
        let resultOption:PopupToastOptions;

        if (typeof option === 'string') {
            resultOption={title:option}
        } else {
            resultOption=option;
        }
        return  resultOption;
    }

}

export {PopupExportComponent}