import { defineComponent, reactive, ref, computed, watch, onMounted, nextTick, resolveComponent, createElementBlock, openBlock, normalizeClass, unref, createVNode, withCtx, mergeProps, normalizeProps, guardReactiveProps, Fragment, renderList, createBlock, renderSlot, createSlots, createElementVNode, createCommentVNode, toDisplayString, createTextVNode } from 'vue';
import { has, omit, isEmpty } from 'lodash-es';
import './config/index.mjs';
import '../../../utils/index.mjs';
import './components/form-component.vue.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';
import { deepUnref } from '../../../utils/deep-unref.mjs';
import _sfc_main$1 from './components/form-component.vue2.mjs';
import { formEmits, formProps } from './config/form.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlForm" },
  __name: "form",
  props: formProps,
  emits: formEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace("form");
    const formData = reactive(props.formData);
    const formRef = ref();
    const formComponents = ref(
      []
    );
    const computedFormColumns = computed(() => {
      const formColumns = deepUnref(props.formColumns);
      return formColumns.map((e) => {
        e.disabled = has(e, "disabled") ? e.disabled : props.disabled;
        if (e.children && e.children.length) {
          e.children.map((a) => {
            a.disabled = has(a, "disabled") ? a.disabled : props.disabled;
            return a;
          });
        }
        return e;
      });
    });
    const validateFields = computed(() => {
      return props.formColumns.filter((e) => e.required || has(e, "rules") && e.rules.length > 0).map((e) => {
        return e.key;
      });
    });
    const computedFormOptions = computed(() => {
      return {
        scrollToFirstError: !props.validateErrorStop,
        ...props.formOptions
      };
    });
    const id = 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 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 });
    };
    watch(
      () => props.formData,
      async (val) => {
        for (const i in val) {
          formData[i] = val[i];
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    onMounted(async () => {
      await 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 = resolveComponent("ll-tooltip");
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_a_col = resolveComponent("a-col");
      const _component_a_row = resolveComponent("a-row");
      const _component_ll_form_item = resolveComponent("ll-form-item");
      const _component_a_form = resolveComponent("a-form");
      const _component_a_spin = resolveComponent("a-spin");
      return openBlock(), createElementBlock(
        "div",
        {
          class: normalizeClass(unref(bem).b())
        },
        [
          createVNode(_component_a_spin, {
            spinning: props.loading
          }, {
            default: withCtx(() => [
              createVNode(_component_a_form, mergeProps(computedFormOptions.value, {
                id: id.value,
                ref_key: "formRef",
                ref: formRef,
                model: props.formData
              }), {
                default: withCtx(() => [
                  createVNode(
                    _component_a_row,
                    normalizeProps(guardReactiveProps(props.rowOptions)),
                    {
                      default: withCtx(() => [
                        (openBlock(true), createElementBlock(
                          Fragment,
                          null,
                          renderList(computedFormColumns.value, (item, index) => {
                            return openBlock(), createBlock(_component_a_col, mergeProps({ key: index }, { ref_for: true }, item?.colOptions ?? {}, {
                              span: item?.spanCol ?? 24
                            }), {
                              default: withCtx(() => [
                                renderSlot(_ctx.$slots, "formItem", {
                                  item,
                                  index
                                }, () => [
                                  createVNode(_component_ll_form_item, mergeProps({ ref_for: true }, getBindValue(item), {
                                    name: item?.key ?? index,
                                    class: [getFormItemClass(item), item.class]
                                  }), createSlots({
                                    label: withCtx(() => [
                                      renderSlot(_ctx.$slots, "label", {
                                        item,
                                        index
                                      }, () => [
                                        createElementVNode(
                                          "span",
                                          null,
                                          toDisplayString(item.label),
                                          1
                                          /* TEXT */
                                        ),
                                        item?.tip ? (openBlock(), createElementBlock(
                                          "span",
                                          {
                                            key: 0,
                                            class: normalizeClass(unref(bem).e("tooltip-wrapper"))
                                          },
                                          [
                                            createVNode(_component_ll_tooltip, mergeProps({
                                              style: { "margin-left": "8px" },
                                              "overlay-class-name": 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 */
                                        )) : createCommentVNode("v-if", true),
                                        item?.info ? (openBlock(), createElementBlock(
                                          "span",
                                          {
                                            key: 1,
                                            class: normalizeClass(unref(bem).e("tooltip-wrapper"))
                                          },
                                          [
                                            createVNode(_component_ll_tooltip, mergeProps({
                                              style: { "margin-left": "8px" },
                                              "overlay-class-name": 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: withCtx(() => [
                                                createVNode(_component_ll_icon, {
                                                  "icon-name": "icon-information",
                                                  size: 14
                                                })
                                              ]),
                                              _: 1
                                              /* STABLE */
                                            }, 16, ["overlay-class-name", "max-width", "placement", "tip"])
                                          ],
                                          2
                                          /* CLASS */
                                        )) : createCommentVNode("v-if", true)
                                      ])
                                    ]),
                                    default: withCtx(() => [
                                      renderSlot(_ctx.$slots, "content", {
                                        item,
                                        index
                                      }, () => [
                                        !unref(isEmpty)(item.children) ? (openBlock(), createBlock(
                                          _component_a_row,
                                          mergeProps({
                                            key: 0,
                                            gutter: 5
                                          }, { ref_for: true }, item.rowOptions),
                                          {
                                            default: withCtx(() => [
                                              (openBlock(true), createElementBlock(
                                                Fragment,
                                                null,
                                                renderList(item.children, (child, childIndex) => {
                                                  return openBlock(), createBlock(
                                                    _component_a_col,
                                                    mergeProps({
                                                      key: childIndex,
                                                      style: {
                                                        display: "flex",
                                                        "align-items": "center"
                                                      }
                                                    }, { ref_for: true }, item?.colOptions ?? {}),
                                                    {
                                                      default: withCtx(() => [
                                                        renderSlot(_ctx.$slots, "children", {
                                                          item: child,
                                                          index
                                                        }, () => [
                                                          createVNode(_sfc_main$1, {
                                                            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 */
                                        )) : (openBlock(), createBlock(_sfc_main$1, {
                                          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: withCtx(() => [
                                        renderSlot(_ctx.$slots, "extra", {
                                          item,
                                          index
                                        }, () => [
                                          createTextVNode(
                                            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 */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=form.vue2.mjs.map
