"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index$1 = require("../subTitle/index.js");
const index = require("../h2/index.js");
const common = require("../../../utils/common.js");
const parseRouteParams = require("../../../utils/parseRouteParams.js");
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 = Vue.inject("formData");
    Vue.inject("getFormData");
    Vue.inject("rowHeight");
    Vue.inject("gridLayout");
    const setFormItemHeight = Vue.inject("setFormItemHeight");
    const subFormData = Vue.computed({
      get: () => {
        if (props.config.dataSourceFrom == "self") {
          return common.cloneDeep(subFormParams.value);
        }
        return common.cloneDeep(formData.value);
      },
      set: (val) => {
        if (props.config.dataSourceFrom != "self") {
          formData.value = { ...formData.value, ...val };
        }
      }
    });
    const fieldList = Vue.computed(() => {
      return [
        ...props.localConfig.fieldsData.map((item) => item.component),
        ...props.localConfig.hiddenFields
      ];
    });
    const subFormParams = Vue.computed(() => {
      if (props.config.dataSourceFrom == "self") {
        const params = parseRouteParams.parseRouteParams(
          props.config.subFormParams,
          formData.value,
          fieldList.value
        );
        return params;
      }
      return {};
    });
    const templateKey = Vue.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 = Vue.ref();
    __expose({
      subFormRef: formRef
    });
    return (_ctx, _cache) => {
      const _component_fec_form = Vue.resolveComponent("fec-form");
      return Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, null, [
        __props.config.titleMode != "none" ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
          __props.config.titleMode == "label" ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index.H2), {
            key: 0,
            config: __props.config
          }, null, 8, ["config"])) : __props.config.titleMode == "subLabel" ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.SubTitle), {
            key: 1,
            config: __props.config
          }, null, 8, ["config"])) : Vue.createCommentVNode("", true)
        ])) : Vue.createCommentVNode("", true),
        Vue.createVNode(_component_fec_form, {
          ref_key: "formRef",
          ref: formRef,
          templateKey: Vue.unref(templateKey),
          isSubForm: "",
          onLayoutUpdated: subFormLoaded,
          canPageEvent: __props.config.dataSourceFrom == "self",
          initFormMode: __props.formMode,
          modelValue: Vue.unref(subFormData),
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.isRef(subFormData) ? subFormData.value = $event : null)
        }, null, 8, ["templateKey", "canPageEvent", "initFormMode", "modelValue"])
      ], 64);
    };
  }
});
exports.default = _sfc_main;
