import { inject, computed, ref, resolveComponent, createElementBlock, openBlock, Fragment, createCommentVNode, createVNode, createBlock, unref, isRef } from "vue";
import { SubTitle } from "../subTitle/index.mjs";
import { H2 } from "../h2/index.mjs";
import { cloneDeep } from "../../../utils/common.mjs";
import { parseRouteParams } from "../../../utils/parseRouteParams.mjs";
const _hoisted_1 = {
  key: 0,
  style: { "height": "60px", "position": "relative" }
};
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "SubForm",
  props: {
    config: {
      type: Object,
      required: true
    },
    localConfig: {
      type: Object,
      required: true
    },
    formMode: {
      type: String,
      default: ""
    }
    //   subFormId: {
    //     type: String,
    //     required: true,
    //   },
    //   subFormKey: {
    //     type: String,
    //     default: "",
    //   },
    //   subFormName: {
    //     type: String,
    //     default: "",
    //   },
    //   dataSourceFrom: {
    //     type: String,
    //     default: "",
    //   },
    //   triggerSubFormPageEvent: {
    //     type: Boolean,
    //     default: false,
    //   },
  },
  setup(__props, { expose: __expose }) {
    const props = __props;
    const formData = inject("formData");
    inject("getFormData");
    inject("rowHeight");
    inject("gridLayout");
    const setFormItemHeight = inject("setFormItemHeight");
    const subFormData = computed({
      get: () => {
        if (props.config.dataSourceFrom == "self") {
          return cloneDeep(subFormParams.value);
        }
        return cloneDeep(formData.value);
      },
      set: (val) => {
        if (props.config.dataSourceFrom != "self") {
          formData.value = { ...formData.value, ...val };
        }
      }
    });
    const fieldList = computed(() => {
      return [
        ...props.localConfig.fieldsData.map((item) => item.component),
        ...props.localConfig.hiddenFields
      ];
    });
    const subFormParams = computed(() => {
      if (props.config.dataSourceFrom == "self") {
        const params = parseRouteParams(
          props.config.subFormParams,
          formData.value,
          fieldList.value
        );
        return params;
      }
      return {};
    });
    const templateKey = computed(() => {
      if (props.config.formSource == "dynamic") {
        const dynamicSubFormKey = props.config.dynamicSubFormKey;
        if (dynamicSubFormKey) {
          if (typeof dynamicSubFormKey === "object") {
            const fieldId = dynamicSubFormKey.value;
            const fieldsData = props.localConfig.fieldsData.map((item) => {
              return item.component;
            });
            const hiddenFields = props.localConfig.hiddenFields;
            const formItem = [...fieldsData, ...hiddenFields].find(
              (item) => item.id == fieldId
            );
            if (formItem != null) {
              const key = formData.value[formItem.fieldName];
              return key;
            } else {
              return "";
            }
          } else {
            return dynamicSubFormKey;
          }
        } else {
          return "";
        }
      } else {
        return props.config.subFormKey;
      }
    });
    function subFormLoaded(fieldsData) {
      const maxYField = fieldsData.reduce(
        (max, field) => field.y > max.y ? field : max,
        { y: 0, h: 0 }
      );
      const maxY = maxYField.y;
      const maxYH = maxYField.h;
      const foundItem = props.localConfig.fieldsData.find(
        (item) => item.id == props.config.id
      );
      const titleHeight = props.config.titleMode != "none" ? 60 : 0;
      if (foundItem) {
        foundItem.fixedH = true;
        foundItem.h = maxY + maxYH + titleHeight;
        setFormItemHeight(props.config.id, true, maxY + maxYH + titleHeight);
      }
    }
    const formRef = ref();
    __expose({
      subFormRef: formRef
    });
    return (_ctx, _cache) => {
      const _component_fec_form = resolveComponent("fec-form");
      return openBlock(), createElementBlock(Fragment, null, [
        __props.config.titleMode != "none" ? (openBlock(), createElementBlock("div", _hoisted_1, [
          __props.config.titleMode == "label" ? (openBlock(), createBlock(unref(H2), {
            key: 0,
            config: __props.config
          }, null, 8, ["config"])) : __props.config.titleMode == "subLabel" ? (openBlock(), createBlock(unref(SubTitle), {
            key: 1,
            config: __props.config
          }, null, 8, ["config"])) : createCommentVNode("", true)
        ])) : createCommentVNode("", true),
        createVNode(_component_fec_form, {
          ref_key: "formRef",
          ref: formRef,
          templateKey: unref(templateKey),
          isSubForm: "",
          onLayoutUpdated: subFormLoaded,
          canPageEvent: __props.config.dataSourceFrom == "self",
          initFormMode: __props.formMode,
          modelValue: unref(subFormData),
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(subFormData) ? subFormData.value = $event : null)
        }, null, 8, ["templateKey", "canPageEvent", "initFormMode", "modelValue"])
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
