import "../../../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                                */
/* empty css                                                                    */
/* empty css                                                                     */
import { computed, getCurrentInstance, inject, createElementBlock, openBlock, createVNode, createCommentVNode, mergeProps, toHandlers, createSlots, withCtx, createElementVNode, toDisplayString, createBlock, createTextVNode, unref } from "vue";
/* empty css                                                                    */
/* empty css                                                                      */
/* empty css                                                                      */
import { openDialog } from "../../dialog/index.mjs";
/* empty css                                                     */
/* empty css                                                           */
/* empty css                                                      */
/* empty css                                                     */
/* empty css                                                        */
/* empty css                                                       */
/* empty css                                                                  */
/* empty css                                                        */
/* empty css                                                      */
/* empty css                                                       */
import api from "../../../api/index.mjs";
/* empty css                                                                      */
/* empty css                                                                          */
import { useEventFlow } from "../../../utils/eventFlow/eventFlowHandler.mjs";
import { getCurrentVueInstance } from "../../../utils/getInstance.mjs";
/* empty css           */
import _export_sfc from "../../../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElInput } from "../../../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElButton } from "../../../../../../node_modules/element-plus/es/components/button/index.mjs";
const _hoisted_1 = { class: "text-field-wrapper" };
const _hoisted_2 = { class: "prefix-content" };
const _hoisted_3 = { class: "suffix-content" };
const _hoisted_4 = {
  key: 0,
  class: "data-masking"
};
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "Text",
  props: {
    config: {
      type: Object,
      required: true
    },
    modelValue: {
      type: [String, Number],
      default: ""
    },
    placeholder: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    maxLength: {
      type: Number,
      default: 1e3
    },
    showWordCount: {
      type: Boolean,
      default: false
    },
    prefix: {
      type: String,
      default: ""
    },
    suffix: {
      type: String,
      default: ""
    },
    suffixButton: {
      type: String,
      default: ""
    },
    suffixButtonName: {
      type: String,
      default: ""
    },
    suffixButtonConfig: {
      type: Array,
      default: []
    },
    suffixDialogId: {
      type: String,
      default: ""
    },
    label: {
      type: String,
      default: ""
    },
    dataMasking: {
      type: String,
      default: ""
    },
    formMode: {
      type: String,
      default: ""
    },
    dialogSource: {
      type: String,
      default: ""
    },
    selectionMode: {
      type: String,
      default: ""
    },
    displayFormatText: {
      type: String,
      default: ""
    },
    displayFormat: {
      type: Object,
      default: {}
    },
    fieldMapping: {
      type: Array,
      default: []
    },
    callbackEventConfig: {
      type: Object,
      default: {}
    }
  },
  emits: ["update:modelValue", "change"],
  setup(__props, { emit: __emit }) {
    const ctx = getCurrentVueInstance();
    const props = __props;
    const emit = __emit;
    const inputValue = computed({
      get: () => props.modelValue,
      set: (val) => {
        emit("update:modelValue", val);
      }
    });
    const realMaxLength = computed(() => {
      return Math.floor(props.maxLength / (ctx.$dbFieldLengthRatio || 1));
    });
    function handleChange(val) {
      emit("change", val);
    }
    const maskedValue = computed(() => {
      if (!props.dataMasking || !props.modelValue) return "";
      return "****";
    });
    const instance = getCurrentInstance();
    const formData = inject("formData", null);
    const localConfig = inject("localConfig", null);
    const fieldsData = inject("fieldsData", null);
    const hiddenFields = inject("hiddenFields", null);
    const formCtx = inject("formCtx", null);
    const fieldsList = [
      ...(fieldsData == null ? void 0 : fieldsData.value) || [],
      ...(hiddenFields == null ? void 0 : hiddenFields.value) || []
    ];
    const { getEventHandlers: createEventHandlers } = useEventFlow({
      fields: fieldsList,
      instance,
      localConfig: localConfig == null ? void 0 : localConfig.value,
      components: {
        form: formCtx
      }
    });
    const getEventHandlers = (row, item, eventConfig) => createEventHandlers(item, row, eventConfig);
    async function openSuffixDialog() {
      let dialog = null;
      if (props.dialogSource == "global") {
        const data = await api.getTemplateDialogById(
          ctx.$http,
          props.suffixDialogId
        );
        dialog = JSON.parse(data.config);
        dialog.id = data.id;
        dialog.name = data.name;
        dialog.selectionMode = props.selectionMode;
        dialog.displayFormat = props.displayFormat;
        dialog.displayFormatText = props.displayFormatText;
        dialog.fieldMapping = props.fieldMapping;
        dialog.callbackEventConfig = props.callbackEventConfig;
      } else if (props.dialogSource == "custom") {
        const dialogsConfig = localConfig.value.dialogs || [];
        dialog = dialogsConfig.find((item) => item.id == props.suffixDialogId);
      }
      if (dialog) {
        openDialog(
          props.config.fieldName,
          dialog,
          instance,
          formCtx,
          fieldsList,
          formData.value
        ).then((result) => {
          console.log("确定", result);
        }).catch(() => {
          console.log("取消");
        });
      } else {
        console.error(`对话框配置不存在:${props.suffixDialogId}`);
      }
    }
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_input = ElInput;
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_component_el_input, mergeProps({
          modelValue: inputValue.value,
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => inputValue.value = $event)
        }, toHandlers(__props.config.eventHandlers), {
          placeholder: __props.placeholder,
          disabled: __props.disabled,
          readonly: __props.suffixButton == "dialog" ? true : false,
          maxlength: realMaxLength.value,
          "show-word-limit": __props.showWordCount,
          onChange: handleChange,
          clearable: ""
        }), createSlots({ _: 2 }, [
          __props.prefix ? {
            name: "prefix",
            fn: withCtx(() => [
              createElementVNode("span", _hoisted_2, toDisplayString(__props.prefix), 1)
            ]),
            key: "0"
          } : void 0,
          __props.suffix ? {
            name: "suffix",
            fn: withCtx(() => [
              createElementVNode("span", _hoisted_3, toDisplayString(__props.suffix), 1)
            ]),
            key: "1"
          } : void 0,
          __props.formMode != "query" && __props.suffixButton && __props.suffixButton != "none" ? {
            name: "append",
            fn: withCtx(() => [
              __props.suffixButton == "dialog" ? (openBlock(), createBlock(_component_el_button, {
                key: 0,
                class: "suffixButton",
                onClick: openSuffixDialog
              }, {
                default: withCtx(() => _cache[1] || (_cache[1] = [
                  createTextVNode("选择")
                ])),
                _: 1
              })) : __props.suffixButton == "custom" ? (openBlock(), createBlock(_component_el_button, mergeProps({
                key: 1,
                class: "suffixButton"
              }, toHandlers(getEventHandlers(unref(formData), {}, __props.suffixButtonConfig))), {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(__props.suffixButtonName), 1)
                ]),
                _: 1
              }, 16)) : createCommentVNode("", true)
            ]),
            key: "2"
          } : void 0
        ]), 1040, ["modelValue", "placeholder", "disabled", "readonly", "maxlength", "show-word-limit"]),
        __props.dataMasking ? (openBlock(), createElementBlock("div", _hoisted_4, toDisplayString(maskedValue.value), 1)) : createCommentVNode("", true)
      ]);
    };
  }
});
const _Text = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-b509ac94"]]);
export {
  _Text as default
};
