"use strict";
const vue = require("vue");
const provide = require("./provide.js");
const types = require("./types.js");
const form = require("./form.js");
const is = require("../_utils/is.js");
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "OForm",
  props: types.formProps,
  emits: ["submit", "validate", "clear", "reset"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const align = vue.computed(() => form.getFlexValue(props.labelAlign));
    const justify = vue.computed(() => form.getFlexValue(props.labelJustify));
    const filedList = [];
    const doValidate = (filed) => {
      const filedNames = filed ? [].concat(filed) : [];
      const list = filedList.map((item) => {
        if (filedNames.length === 0 || item.filed && filedNames.includes(item.filed)) {
          return item.validate ? item.validate() : null;
        }
        return null;
      });
      return Promise.all(list).then((rlt) => {
        emits("validate", rlt);
        return rlt;
      });
    };
    const clearValidate = (filed, onClear) => {
      const filedNames = filed ? [].concat(filed) : [];
      filedList.forEach((item) => {
        if (filedNames.length === 0 || item.filed && filedNames.includes(item.filed)) {
          item.clearValidate();
          if (is.isFunction(onClear)) {
            onClear(item);
          }
        }
      });
      emits("clear", filed);
    };
    const addFiled = (filedItem) => {
      filedList.push(filedItem);
    };
    const removeFiled = (filed) => {
      const idx = filedList.findIndex((item) => item.filed === filed);
      filedList.splice(idx, 1);
    };
    const resetFields = (filed) => {
      clearValidate(filed, (item) => {
        item.resetFiled();
      });
      emits("reset", filed);
    };
    const onSubmit = () => {
      doValidate().then((rlt) => {
        emits("submit", rlt);
      });
    };
    vue.provide(provide.formInjectKey, {
      model: vue.computed(() => props.model),
      addFiled,
      removeFiled
    });
    __expose({
      validate: doValidate,
      resetFields,
      clearValidate
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(
        "form",
        {
          class: vue.normalizeClass(["o-form", [
            {
              "o-form-has-required": props.hasRequired
            },
            `o-form-layout-${props.layout}`
          ]]),
          style: vue.normalizeStyle({
            "--form-label-width": props.labelWidth,
            "--form-label-align": props.labelAlign,
            "--form-label-justify": justify.value,
            "--form-item-align": align.value
          }),
          onSubmit: vue.withModifiers(onSubmit, ["prevent"])
        },
        [
          vue.renderSlot(_ctx.$slots, "default")
        ],
        38
        /* CLASS, STYLE, NEED_HYDRATION */
      );
    };
  }
});
module.exports = _sfc_main;
