"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
require("../../../node_modules/element-plus/es/index.js");
const ValueSelectorPopover = require("./ValueSelectorPopover.vue2.js");
const common = require("../packages/utils/common.js");
;/* empty css                    */
const _pluginVue_exportHelper = require("../../../_virtual/_plugin-vue_export-helper.js");
const index = require("../../../node_modules/element-plus/es/components/input-tag/index.js");
const index$1 = require("../../../node_modules/element-plus/es/components/input/index.js");
const _sfc_main = {
  __name: "ValueSelector",
  props: {
    // modelValue 的格式：
    // 单选：{ type: 'field', value: '字段ID' }
    // 多选：[{ type: 'field', value: '字段ID1' }, { type: 'field', value: '字段ID2' }]
    modelValue: {
      default: () => null
    },
    placeholder: {
      type: String,
      default: "请输入或选择值"
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: "small"
    }
    // // 可选的字段列表（从父组件传入）
    // availableFields: {
    //   type: Array,
    //   default: () => [],
    //   validator: (value) => Array.isArray(value),
    // },
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const inputComponent = Vue.computed(() => {
      return props.multiple ? index.ElInputTag : index$1.ElInput;
    });
    const hiddenFieldsData = Vue.computed(() => {
      const editConfigData = common.getEditConfigData();
      return (editConfigData == null ? void 0 : editConfigData.hiddenFields) || [];
    });
    const availableFields = Vue.computed(() => {
      const fields = common.getEditConfigDataFields() || [];
      return fields.map((item) => ({
        id: item.id,
        label: item.label,
        fieldName: item.fieldName,
        fieldType: item.fieldType
      }));
    });
    const displayText = Vue.computed(() => {
      if (!props.modelValue) return "";
      if (!props.multiple && typeof props.modelValue === "object" && !Array.isArray(props.modelValue)) {
        const val = props.modelValue;
        if (val.label) return val.label;
        const field = availableFields.value.find((f) => f.id === val.value);
        return (field == null ? void 0 : field.label) || (field == null ? void 0 : field.fieldName) || val.value;
      } else if (!props.multiple && typeof props.modelValue === "string") {
        return props.modelValue;
      }
      return "";
    });
    const tags = Vue.computed({
      get() {
        if (!props.multiple || !Array.isArray(props.modelValue)) {
          return [];
        }
        return props.modelValue.map((item) => {
          if (item.label) return item.label;
          const field = availableFields.value.find((f) => f.id === item.value);
          return (field == null ? void 0 : field.label) || (field == null ? void 0 : field.fieldName) || item.value;
        }).filter(Boolean);
      },
      set(val) {
        if (Array.isArray(props.modelValue)) {
          const newModelValue = props.modelValue.filter((item) => {
            const itemLabel = item.label || (() => {
              const field = [
                ...availableFields.value,
                ...hiddenFieldsData.value
              ].find((f) => f.id === item.value);
              return (field == null ? void 0 : field.label) || (field == null ? void 0 : field.fieldName) || item.value;
            })();
            return val.includes(itemLabel);
          });
          emit("update:modelValue", newModelValue);
        }
      }
    });
    const unifiedInputValue = Vue.computed({
      get() {
        return props.multiple ? tags.value : displayText.value;
      },
      set(val) {
        if (props.multiple && Array.isArray(val)) {
          tags.value = val;
        } else if (!props.multiple && typeof val === "string") {
          emit("update:modelValue", val);
        }
      }
    });
    const handleConfirm = (selected) => {
      let result = null;
      if (selected.type === "field") {
        const field = [...availableFields.value, ...hiddenFieldsData.value].find(
          (f) => f.id === selected.value
        );
        result = {
          label: `\${${(field == null ? void 0 : field.label) || (field == null ? void 0 : field.fieldName) || selected.value}}`,
          type: "field",
          value: selected.value
        };
      } else if (selected.type === "userInfo") {
        result = {
          label: `\${用户信息: ${getUserInfoLabel(selected.value)}}`,
          type: "userInfo",
          value: selected.value
        };
      } else if (selected.type === "urlParam") {
        result = {
          label: `\${路由参数: ${selected.value}}`,
          type: "urlParam",
          value: selected.value
        };
      }
      if (!result) return;
      if (props.multiple) {
        const currentValue = Array.isArray(props.modelValue) ? props.modelValue : [];
        const exists = currentValue.some((item) => item.value === result.value);
        if (!exists) {
          emit("update:modelValue", [...currentValue, result]);
        }
      } else {
        emit("update:modelValue", result);
      }
    };
    const getUserInfoLabel = (key) => {
      const labels = {
        userCode: "用户编号",
        userName: "用户名称",
        roleCode: "角色编号",
        roleName: "角色名称",
        orgCode: "机构编号",
        orgName: "机构名称"
      };
      return labels[key] || key;
    };
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(inputComponent.value), {
        modelValue: unifiedInputValue.value,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => unifiedInputValue.value = $event),
        placeholder: __props.placeholder,
        disabled: __props.disabled,
        readonly: __props.readOnly,
        trigger: __props.multiple ? "Space" : void 0,
        "collapse-tags": "",
        "collapse-tags-tooltip": "",
        "max-collapse-tags": 1,
        class: "value-selector-input",
        size: __props.size
      }, {
        suffix: Vue.withCtx(() => [
          Vue.createVNode(ValueSelectorPopover.default, {
            "available-fields": availableFields.value,
            disabled: __props.disabled,
            onConfirm: handleConfirm
          }, null, 8, ["available-fields", "disabled"])
        ]),
        _: 1
      }, 8, ["modelValue", "placeholder", "disabled", "readonly", "trigger", "size"]);
    };
  }
};
const ValueSelector = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-e5b87efa"]]);
exports.default = ValueSelector;
