import { isVNode as A, defineComponent as E, computed as m, useSlots as L, shallowRef as G, ref as h, watch as H, unref as i, cloneVNode as J, createVNode as o, mergeProps as c, openBlock as b, createElementBlock as K, Fragment as Q, withCtx as f, renderSlot as w, createBlock as B, createCommentVNode as R, createTextVNode as X, toDisplayString as Y } from "vue";
import { Button as F, Flex as Z, Space as ee, Drawer as te } from "@aplus-frontend/antdv";
import { omit as ae, isBoolean as le, isUndefined as ie } from "lodash-unified";
import "../../render/index.mjs";
import "../../config-provider/index.mjs";
import "../../hooks/index.mjs";
import { getNumberPx as g, convertReactiveToRaw as ne } from "../../utils/index.mjs";
import { getSingleVNode as oe } from "../../utils/slot.mjs";
import "../ap-form.vue.mjs";
import se from "../style/drawer-form.mjs";
import { useControllableValue as re } from "../../hooks/useControllableValue.mjs";
import { useLocale as ue } from "../../config-provider/hooks/use-locale.mjs";
import { useNamespace as de } from "../../config-provider/hooks/use-namespace.mjs";
import me from "../ap-form.vue2.mjs";
import x from "../../render/render.mjs";
function O(s) {
  return typeof s == "function" || Object.prototype.toString.call(s) === "[object Object]" && !A(s);
}
const Ie = /* @__PURE__ */ E({
  name: "ApFormDrawerForm",
  __name: "index",
  props: {
    layout: {
      default: "horizontal"
    },
    labelCol: {},
    wrapperCol: {},
    colon: {
      type: Boolean,
      default: !0
    },
    labelAlign: {
      default: "right"
    },
    labelWrap: {
      type: Boolean
    },
    prefixCls: {},
    requiredMark: {},
    hideRequiredMark: {
      type: Boolean,
      default: !1
    },
    rules: {},
    validateMessages: {},
    validateOnRuleChange: {
      type: Boolean,
      default: !0
    },
    scrollToFirstError: {},
    onSubmit: {},
    name: {},
    validateTrigger: {
      default: "change"
    },
    size: {},
    disabled: {
      type: Boolean
    },
    onFieldsChange: {},
    onFinishFailed: {},
    onValidate: {},
    variant: {},
    initialValues: {},
    onValuesChange: {},
    syncToUrl: {
      type: [Boolean, Function]
    },
    syncToUrlPriority: {
      type: Boolean
    },
    autoFocusFirstInput: {
      type: Boolean
    },
    open: {
      type: Boolean,
      default: void 0
    },
    "onUpdate:open": {},
    drawerProps: {
      default: () => ({})
    },
    title: {},
    width: {
      default: 378
    },
    onFinish: {},
    submitter: {
      type: [Boolean, Object],
      default: () => ({
        resetButtonProps: !1
      })
    },
    showCancel: {
      type: Boolean,
      default: !0
    },
    submitTimeout: {},
    resize: {
      type: [Object, Boolean],
      default: !1
    }
  },
  emits: ["update:open"],
  setup(s, {
    expose: k,
    emit: W
  }) {
    const t = s, P = m(() => ae(t, ["drawerProps", "open", "onUpdate:open", "title", "width", "onFinish", "submitter"])), p = L(), N = W, l = G(), r = h(), n = h(!1), {
      value: S,
      updateValue: u
    } = re(t, N, {
      valuePropName: "open",
      defaultValue: !1
    }), {
      t: v
    } = ue(), _ = se("ap-form-drawer-form"), {
      b: I
    } = de("ap-form-drawer-form");
    H(() => l.value, (e) => {
      e && r.value && (e.setFieldsValue?.(r.value), r.value = void 0);
    });
    const d = m(() => {
      const e = {
        onResize: () => {
        },
        minWidth: 300,
        maxWidth: window.innerWidth * 0.8,
        resizeOnOpen: !0
      };
      return le(t.resize) ? t.resize ? e : {} : {
        minWidth: t.resize?.minWidth ?? e.minWidth,
        maxWidth: t.resize?.maxWidth ?? e.maxWidth,
        onResize: t.resize?.onResize ?? e.onResize,
        resizeOnOpen: ie(t.resize?.resizeOnOpen) ? e.resizeOnOpen : t.resize.resizeOnOpen
      };
    });
    function j() {
      const e = i(d)?.minWidth;
      return !e || e && e < t.width ? t.width : e;
    }
    const y = h(g(j()));
    function U(e) {
      const {
        resizeOnOpen: a,
        onResize: T
      } = d.value;
      a && (y.value = e, T?.(e));
    }
    const z = m(() => {
      const e = oe(p.trigger);
      return e ? J(e, {
        onClick: () => {
          u(!0);
        }
      }) : null;
    }), V = m(() => {
      const e = [];
      if (t.submitter === !1)
        return null;
      if (t.showCancel) {
        let a;
        e.push(o(F, {
          onClick: C,
          disabled: n.value
        }, O(a = v("ap.common.cancelText")) ? a : {
          default: () => [a]
        }));
      }
      return t.submitter?.resetButtonProps !== !1 && e.push(o(F, c(t.submitter?.resetButtonProps || {}, {
        key: "reset",
        disabled: n.value,
        onClick: M
      }), {
        default: () => [t.submitter?.resetText || v("ap.common.resetText")]
      })), t.submitter?.submitButtonProps !== !1 && e.push(o(F, c({
        type: "primary",
        key: "submit"
      }, t.submitter?.submitButtonProps || {}, {
        loading: n.value,
        onClick: D
      }), {
        default: () => [t.submitter?.submitText || v("ap.common.okText")]
      })), o(Z, {
        justify: "flex-end"
      }, {
        default: () => [p.submitter ? p.submitter(e) : o(ee, null, O(e) ? e : {
          default: () => [e]
        })]
      });
    });
    function M() {
      t.submitter !== !1 && (l.value?.resetFields(), t.submitter?.onReset?.());
    }
    async function D() {
      if (t.submitter !== !1)
        try {
          const e = await l.value?.validateFieldsReturnTransformed();
          t.submitter?.onSubmit?.(), n.value = !0;
          const a = t.onFinish?.(ne(e));
          if (t.submitTimeout && a instanceof Promise) {
            const q = setTimeout(() => n.value = !1, t.submitTimeout);
            a.finally(() => {
              clearTimeout(q), n.value = !1;
            });
          }
          await a && u(!1);
        } finally {
          n.value = !1;
        }
    }
    function C() {
      i(n) || u(!1);
    }
    function $(e) {
      !e && i(n) || u(e);
    }
    return k({
      resetFields: async () => {
        l.value?.resetFields();
      },
      clearValidate: (e) => {
        l.value?.clearValidate(e);
      },
      validateFields: (e, a) => l.value?.validateFields(e, a),
      getFieldsValue: (e) => l.value?.getFieldsValue(e),
      validate: (e, a) => l.value?.validate(e, a),
      scrollToField: (e, a = {}) => {
        l.value?.scrollToField(e, a);
      },
      setFieldValue: (e, a) => {
        l.value?.setFieldValue?.(e, a);
      },
      setFieldsValue: (e) => {
        if (!l.value) {
          r.value = e;
          return;
        }
        l.value?.setFieldsValue?.(e);
      },
      getInternalInstance: (e) => l.value?.getInternalInstance(e),
      getFieldsValueTransformed: (e) => l.value?.getFieldsValueTransformed(e),
      validateFieldsReturnTransformed: (e) => l.value?.validateFieldsReturnTransformed(e)
    }), (e, a) => (b(), K(Q, null, [o(i(te), c(s.drawerProps, {
      open: i(S),
      size: y.value,
      "max-size": i(g)(d.value.maxWidth),
      "min-size": i(g)(d.value.minWidth),
      resizable: {
        onResize: U
      },
      class: [i(I)(), i(_)],
      "onUpdate:open": $,
      onCancel: C
    }), {
      title: f(() => [w(e.$slots, "title", {}, () => [X(Y(t.title), 1)])]),
      footer: f(() => [V.value ? (b(), B(i(x), {
        key: 0,
        node: V.value
      }, null, 8, ["node"])) : R("", !0)]),
      default: f(() => [o(me, c({
        ref_key: "formRef",
        ref: l
      }, P.value), {
        default: f(() => [w(e.$slots, "default")]),
        _: 3
      }, 16)]),
      _: 3
    }, 16, ["open", "size", "max-size", "min-size", "resizable", "class"]), z.value ? (b(), B(i(x), {
      key: 0,
      node: z.value
    }, null, 8, ["node"])) : R("", !0)], 64));
  }
});
export {
  Ie as default
};
