import { computed, defineAsyncComponent, inject, useAttrs, createBlock, openBlock, resolveDynamicComponent, unref, mergeProps, isRef } from "vue";
import { parseRule } from "../../../utils/formRule.mjs";
const _sfc_main = {
  __name: "FormItem",
  props: {
    fieldType: {
      type: String,
      default: "text"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    placeholder: String
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const formItem = computed(() => {
      switch (props.fieldType) {
        case "text":
          return defineAsyncComponent(() => import("../field/index.mjs"));
        case "picker":
          return defineAsyncComponent(() => import("../fieldPicker/index.mjs"));
        case "cascader":
          return defineAsyncComponent(() => import("../fieldCascaderPicker/index.mjs"));
        case "calendar":
          return defineAsyncComponent(() => import("../fieldCalendarPicker/index.mjs"));
        case "date":
          return defineAsyncComponent(() => import("../fieldDatePicker/index.mjs"));
        case "dateTime":
          return defineAsyncComponent(() => import("../fieldDateTimePicker/index.mjs"));
        case "time":
          return defineAsyncComponent(() => import("../fieldTimePicker/index.mjs"));
        case "radio":
          return defineAsyncComponent(() => import("../fieldRadio/index.mjs"));
        case "checkbox":
          return defineAsyncComponent(() => import("../fieldCheckbox/index.mjs"));
        case "upload":
          return defineAsyncComponent(() => import("../fieldUploader/index.mjs"));
        case "area":
          return defineAsyncComponent(() => import("../fieldArea/index.mjs"));
      }
    });
    const formData = inject("formData");
    const formDisabled = inject("formDisabled");
    const formReadonly = inject("formReadonly");
    const dicListMap = inject("dicListMap");
    const formItemDisabled = computed(() => {
      if (formDisabled == null ? void 0 : formDisabled.value) {
        return formDisabled.value;
      }
      return props.disabled;
    });
    const formItemReadonly = computed(() => {
      if (formReadonly == null ? void 0 : formReadonly.value) {
        return formReadonly.value;
      }
      return props.readonly;
    });
    const attrs = useAttrs();
    const emit = __emit;
    const compValue = computed({
      get: () => {
        var _a;
        if (formData) {
          return (_a = formData.value) == null ? void 0 : _a[attrs.name];
        }
        return props.modelValue;
      },
      set: (val) => {
        if (formData) {
          formData.value[attrs.name] = val;
        } else {
          emit("update:modelValue", val);
        }
      }
    });
    const rules = computed(() => {
      return parseRule(props.fieldType, attrs);
    });
    return (_ctx, _cache) => {
      var _a;
      return openBlock(), createBlock(resolveDynamicComponent(unref(formItem)), mergeProps(_ctx.$attrs, {
        modelValue: unref(compValue),
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(compValue) ? compValue.value = $event : null),
        fieldType: __props.fieldType,
        rules: unref(rules),
        required: unref(formItemReadonly) ? false : "auto",
        disabled: unref(formItemDisabled),
        readonly: unref(formItemReadonly),
        placeholder: unref(formItemReadonly) ? "" : __props.placeholder,
        dicList: (_a = unref(dicListMap)) == null ? void 0 : _a[unref(attrs).dicKey]
      }), null, 16, ["modelValue", "fieldType", "rules", "required", "disabled", "readonly", "placeholder", "dicList"]);
    };
  }
};
export {
  _sfc_main as default
};
