'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var lodashEs = require('lodash-es');
require('./config/index.js');
require('../../../utils/index.js');
require('./components/form-component.vue.js');
var createNamespace = require('../../../utils/create-namespace.js');
var deepUnref = require('../../../utils/deep-unref.js');
var formComponent_vue_vue_type_script_setup_true_lang = require('./components/form-component.vue2.js');
var form = require('./config/form.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlForm" },
  __name: "form",
  props: form.formProps,
  emits: form.formEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("form");
    const formData = vue.reactive(props.formData);
    const formRef = vue.ref();
    const formComponents = vue.ref(
      []
    );
    const computedFormColumns = vue.computed(() => {
      const formColumns = deepUnref.deepUnref(props.formColumns);
      return formColumns.map((e) => {
        e.disabled = lodashEs.has(e, "disabled") ? e.disabled : props.disabled;
        if (e.children && e.children.length) {
          e.children.map((a) => {
            a.disabled = lodashEs.has(a, "disabled") ? a.disabled : props.disabled;
            return a;
          });
        }
        return e;
      });
    });
    const validateFields = vue.computed(() => {
      return props.formColumns.filter((e) => e.required || lodashEs.has(e, "rules") && e.rules.length > 0).map((e) => {
        return e.key;
      });
    });
    const computedFormOptions = vue.computed(() => {
      return {
        scrollToFirstError: !props.validateErrorStop,
        ...props.formOptions
      };
    });
    const id = vue.ref(`ll-form-${+/* @__PURE__ */ new Date()}`);
    const validate = async (nameList) => {
      if (props.validateErrorStop) {
        clearValidate();
        if (!validateFields.value.length) return Promise.resolve();
        for (const i of validateFields.value) {
          try {
            await formRef.value.validateFields([i]);
          } catch (error) {
            return Promise.reject(error);
          }
        }
        return Promise.resolve();
      } else {
        try {
          await formRef.value.validateFields(nameList || validateFields.value);
          return Promise.resolve();
        } catch (error) {
          return Promise.reject(error);
        }
      }
    };
    const resetFields = async () => {
      await formRef.value.resetFields();
    };
    const clearValidate = async (nameList) => {
      await formRef.value.clearValidate(nameList);
    };
    const scrollToField = async (name, options) => {
      await formRef.value.scrollToField(name, options);
    };
    const getBindValue = (item) => {
      const obj = { ...item };
      return lodashEs.omit(obj, ["label", "class", "extra"]);
    };
    const getFormItemClass = (item = {}) => {
      if (props.formOptions.layout === "horizontal") {
        if (item?.labelCol) {
          return item.labelCol.span && item.labelCol.span >= 24 ? bem.is("label-top") : bem.is("label-left");
        } else {
          return bem.is("label-left");
        }
      }
      return bem.is("label-top");
    };
    const handleChangeField = (item) => {
      emits("change", { item, value: formData[item.key], formData });
    };
    const handleBlurField = (item) => {
      emits("blur", { item, value: formData[item.key], formData });
    };
    vue.watch(
      () => props.formData,
      async (val) => {
        for (const i in val) {
          formData[i] = val[i];
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    vue.onMounted(async () => {
      await vue.nextTick();
      if (props.autoFocus) {
        for (const element of formComponents.value) {
          const item = element?.getItem();
          if (["input", "textarea", "input-number"].includes(item.type)) {
            element?.focus();
            break;
          }
        }
      }
    });
    __expose({
      validate,
      resetFields,
      clearValidate,
      scrollToField
    });
    return (_ctx, _cache) => {
      const _component_ll_tooltip = vue.resolveComponent("ll-tooltip");
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_a_col = vue.resolveComponent("a-col");
      const _component_a_row = vue.resolveComponent("a-row");
      const _component_ll_form_item = vue.resolveComponent("ll-form-item");
      const _component_a_form = vue.resolveComponent("a-form");
      const _component_a_spin = vue.resolveComponent("a-spin");
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          class: vue.normalizeClass(vue.unref(bem).b())
        },
        [
          vue.createVNode(_component_a_spin, {
            spinning: props.loading
          }, {
            default: vue.withCtx(() => [
              vue.createVNode(_component_a_form, vue.mergeProps(computedFormOptions.value, {
                id: id.value,
                ref_key: "formRef",
                ref: formRef,
                model: props.formData
              }), {
                default: vue.withCtx(() => [
                  vue.createVNode(
                    _component_a_row,
                    vue.normalizeProps(vue.guardReactiveProps(props.rowOptions)),
                    {
                      default: vue.withCtx(() => [
                        (vue.openBlock(true), vue.createElementBlock(
                          vue.Fragment,
                          null,
                          vue.renderList(computedFormColumns.value, (item, index) => {
                            return vue.openBlock(), vue.createBlock(_component_a_col, vue.mergeProps({ key: index }, { ref_for: true }, item?.colOptions ?? {}, {
                              span: item?.spanCol ?? 24
                            }), {
                              default: vue.withCtx(() => [
                                vue.renderSlot(_ctx.$slots, "formItem", {
                                  item,
                                  index
                                }, () => [
                                  vue.createVNode(_component_ll_form_item, vue.mergeProps({ ref_for: true }, getBindValue(item), {
                                    name: item?.key ?? index,
                                    class: [getFormItemClass(item), item.class]
                                  }), vue.createSlots({
                                    label: vue.withCtx(() => [
                                      vue.renderSlot(_ctx.$slots, "label", {
                                        item,
                                        index
                                      }, () => [
                                        vue.createElementVNode(
                                          "span",
                                          null,
                                          vue.toDisplayString(item.label),
                                          1
                                          /* TEXT */
                                        ),
                                        item?.tip ? (vue.openBlock(), vue.createElementBlock(
                                          "span",
                                          {
                                            key: 0,
                                            class: vue.normalizeClass(vue.unref(bem).e("tooltip-wrapper"))
                                          },
                                          [
                                            vue.createVNode(_component_ll_tooltip, vue.mergeProps({
                                              style: { "margin-left": "8px" },
                                              "overlay-class-name": vue.unref(bem).e("tooltip"),
                                              "arrow-point-at-center": true,
                                              "max-width": item?.tipConfig?.maxWidth ?? 400,
                                              placement: item?.tipConfig?.placement ?? "top",
                                              tip: item.tip
                                            }, { ref_for: true }, item?.tipConfig), null, 16, ["overlay-class-name", "max-width", "placement", "tip"])
                                          ],
                                          2
                                          /* CLASS */
                                        )) : vue.createCommentVNode("v-if", true),
                                        item?.info ? (vue.openBlock(), vue.createElementBlock(
                                          "span",
                                          {
                                            key: 1,
                                            class: vue.normalizeClass(vue.unref(bem).e("tooltip-wrapper"))
                                          },
                                          [
                                            vue.createVNode(_component_ll_tooltip, vue.mergeProps({
                                              style: { "margin-left": "8px" },
                                              "overlay-class-name": vue.unref(bem).e("tooltip"),
                                              "arrow-point-at-center": true,
                                              "max-width": item?.tipConfig?.maxWidth ?? 400,
                                              placement: item?.tipConfig?.placement ?? "top",
                                              tip: item.info
                                            }, { ref_for: true }, item?.tipConfig), {
                                              default: vue.withCtx(() => [
                                                vue.createVNode(_component_ll_icon, {
                                                  "icon-name": "icon-information",
                                                  size: 14
                                                })
                                              ]),
                                              _: 1
                                              /* STABLE */
                                            }, 16, ["overlay-class-name", "max-width", "placement", "tip"])
                                          ],
                                          2
                                          /* CLASS */
                                        )) : vue.createCommentVNode("v-if", true)
                                      ])
                                    ]),
                                    default: vue.withCtx(() => [
                                      vue.renderSlot(_ctx.$slots, "content", {
                                        item,
                                        index
                                      }, () => [
                                        !vue.unref(lodashEs.isEmpty)(item.children) ? (vue.openBlock(), vue.createBlock(
                                          _component_a_row,
                                          vue.mergeProps({
                                            key: 0,
                                            gutter: 5
                                          }, { ref_for: true }, item.rowOptions),
                                          {
                                            default: vue.withCtx(() => [
                                              (vue.openBlock(true), vue.createElementBlock(
                                                vue.Fragment,
                                                null,
                                                vue.renderList(item.children, (child, childIndex) => {
                                                  return vue.openBlock(), vue.createBlock(
                                                    _component_a_col,
                                                    vue.mergeProps({
                                                      key: childIndex,
                                                      style: {
                                                        display: "flex",
                                                        "align-items": "center"
                                                      }
                                                    }, { ref_for: true }, item?.colOptions ?? {}),
                                                    {
                                                      default: vue.withCtx(() => [
                                                        vue.renderSlot(_ctx.$slots, "children", {
                                                          item: child,
                                                          index
                                                        }, () => [
                                                          vue.createVNode(formComponent_vue_vue_type_script_setup_true_lang.default, {
                                                            item: child,
                                                            "form-data": formData,
                                                            onChangeField: handleChangeField,
                                                            onBlurField: handleBlurField
                                                          }, null, 8, ["item", "form-data"])
                                                        ])
                                                      ]),
                                                      _: 2
                                                      /* DYNAMIC */
                                                    },
                                                    1040
                                                    /* FULL_PROPS, DYNAMIC_SLOTS */
                                                  );
                                                }),
                                                128
                                                /* KEYED_FRAGMENT */
                                              ))
                                            ]),
                                            _: 2
                                            /* DYNAMIC */
                                          },
                                          1040
                                          /* FULL_PROPS, DYNAMIC_SLOTS */
                                        )) : (vue.openBlock(), vue.createBlock(formComponent_vue_vue_type_script_setup_true_lang.default, {
                                          key: 1,
                                          ref_for: true,
                                          ref: (el) => formComponents.value[index] = el,
                                          item,
                                          "form-data": formData,
                                          onChangeField: handleChangeField,
                                          onBlurField: handleBlurField
                                        }, null, 8, ["item", "form-data"]))
                                      ])
                                    ]),
                                    _: 2
                                    /* DYNAMIC */
                                  }, [
                                    item.extra ? {
                                      name: "extra",
                                      fn: vue.withCtx(() => [
                                        vue.renderSlot(_ctx.$slots, "extra", {
                                          item,
                                          index
                                        }, () => [
                                          vue.createTextVNode(
                                            vue.toDisplayString(item.extra),
                                            1
                                            /* TEXT */
                                          )
                                        ])
                                      ]),
                                      key: "0"
                                    } : void 0
                                  ]), 1040, ["name", "class"])
                                ])
                              ]),
                              _: 2
                              /* DYNAMIC */
                            }, 1040, ["span"]);
                          }),
                          128
                          /* KEYED_FRAGMENT */
                        ))
                      ]),
                      _: 3
                      /* FORWARDED */
                    },
                    16
                    /* FULL_PROPS */
                  )
                ]),
                _: 3
                /* FORWARDED */
              }, 16, ["id", "model"])
            ]),
            _: 3
            /* FORWARDED */
          }, 8, ["spinning"])
        ],
        2
        /* CLASS */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=form.vue2.js.map
