import { defineComponent as oe, reactive as ae, ref as E, watchEffect as G, watch as te, computed as j, createVNode as a, createTextVNode as L, mergeProps as s, Fragment as K, isVNode as re, renderSlot as ue } from "vue";
import { useMessage as de, NGrid as H, NStep as se, NSpace as Q, NButton as R, NSpin as ce, NForm as J, NSteps as ie, NModal as fe, NDivider as T, NDrawer as pe, NGi as he, NFormItem as W, NTooltip as ye, NIcon as ke, NAutoComplete as ge, NSlider as we, NUpload as ve, NCheckboxGroup as Ve, NCheckbox as X, NColorPicker as Se, NDatePicker as Ce, NTimePicker as Ue, NSwitch as Ne, NRate as be, NSelect as Fe, NRadioGroup as Ee, NRadio as Y, NInputNumber as Pe, NInput as Be } from "naive-ui";
import ze from "./index3.mjs";
import Me from "./index4.mjs";
function f(o) {
  return typeof o == "function" || Object.prototype.toString.call(o) === "[object Object]" && !re(o);
}
const Oe = {
  autoPlaceholder: Boolean,
  autoMessageError: Boolean,
  modal: Boolean,
  modalShow: Boolean,
  modalProps: Object,
  drawer: Boolean,
  drawerShow: Boolean,
  drawerProps: Object,
  formProps: Object,
  formItems: {
    type: Array,
    default: []
  },
  stepsFormItems: {
    type: Array,
    default: []
  },
  resetButton: Boolean,
  validateButton: Boolean,
  submitButton: {
    type: Boolean,
    default: !0
  },
  steps: Boolean,
  spin: Boolean,
  scrollToFirstError: Boolean,
  title: String,
  isKeyPressSubmit: Boolean,
  initialValues: Object,
  grid: Boolean,
  gridConfig: Object,
  modelValue: Object,
  requestConfig: Object,
  transform: Function,
  onReset: Function,
  onFinish: Function,
  onError: Function,
  onValidate: Function,
  onUpdateModalShow: Function,
  "onUpdate:modalShow": Function,
  "onUpdate:modelValue": Function,
  onUpdateModelValue: Function,
  onUpdateDrawerShow: Function,
  "onUpdate:drawerShow": Function,
  onValuesChange: Function
}, De = oe({
  name: "ProForm",
  props: Oe,
  setup(o, {
    slots: x,
    expose: P
  }) {
    const t = ae({}), p = E(1), C = E("process"), y = E(!1), c = E({}), h = () => {
      for (const e in o.initialValues)
        Object.prototype.hasOwnProperty.call(o.initialValues, e) && (t[e] = o.initialValues[e]);
    };
    G(() => {
      if (o.modelValue !== void 0)
        for (const e in o.modelValue)
          Object.prototype.hasOwnProperty.call(o.modelValue, e) && (t[e] = o.modelValue[e]);
    }), te(t, () => {
      o.onUpdateModelValue && o.onUpdateModelValue(t), o["onUpdate:modelValue"] && o["onUpdate:modelValue"](t);
    }), h();
    const w = E(null), k = (e) => {
      const r = document.querySelector(e), l = (r == null ? void 0 : r.getClientRects()[0].top) || 0;
      window.scrollBy({
        top: l - 50,
        behavior: "smooth"
      });
    };
    let U;
    o.autoMessageError && (U = de());
    const B = (e) => {
      U && U.error(e || "");
    }, N = () => {
      var n;
      const {
        scrollToFirstError: e,
        autoMessageError: r,
        onValidate: l,
        onError: u
      } = o;
      (n = w.value) == null || n.validate((d) => {
        d ? (e && d.length > 0 && k(`#n-pro-form-${d[0][0].field}`), r && d.length > 0 && B(d[0][0].message), u && u(d)) : l && l(t);
      });
    }, b = () => {
      for (const e in t)
        Object.prototype.hasOwnProperty.call(t, e) && (o.initialValues ? t[e] = o.initialValues[e] : t[e] = null);
      o != null && o.onReset && o.onReset();
    }, v = () => {
      var F;
      y.value = !0;
      const {
        onFinish: e,
        onError: r,
        transform: l,
        steps: u,
        stepsFormItems: n,
        scrollToFirstError: d,
        autoMessageError: g
      } = o;
      (F = w.value) == null || F.validate(async (S) => {
        if (S)
          u && (C.value = "error"), d && S.length > 0 && k(`#n-pro-form-${S[0][0].field}`), g && S.length > 0 && B(S[0][0].message), r && r(S), y.value = !1;
        else {
          if (u) {
            C.value = "process";
            const le = n.length;
            p.value < le && p.value++;
            return;
          }
          const O = o.requestConfig;
          let I;
          const A = l ? l(t) : t;
          O === void 0 ? I = A : I = await Me(O.methods, O.url, A, O.headers), e && await e(I), y.value = !1;
        }
      });
    };
    P({
      submit: v,
      reset: b,
      validate: N,
      scrollToField: k
    });
    const i = (e, r, l) => {
      t[r] = l ? l(e) : e, o != null && o.onValuesChange && o.onValuesChange(r, e);
      const u = c.value[r];
      u !== void 0 && u.forEach((n) => {
        n(r, e);
      });
    }, z = (e, r, l) => {
      e ? t[r] = l : t[r] = null, o != null && o.onValuesChange && o.onValuesChange(r, e);
    }, D = (e) => {
      o.onUpdateModalShow && o.onUpdateModalShow(e), o["onUpdate:modalShow"] && o["onUpdate:modalShow"](e);
    }, V = (e) => {
      o.onUpdateDrawerShow && o.onUpdateDrawerShow(e), o["onUpdate:drawerShow"] && o["onUpdate:drawerShow"](e);
    }, $ = (e) => {
      e.key === "Enter" && v();
    };
    G(() => {
      o.isKeyPressSubmit ? window.addEventListener("keydown", $) : window.removeEventListener("keydown", $);
    });
    const q = (e) => {
      var r;
      if (!(e.type === "divider" || e.type === "slot"))
        return (r = e.tooltipConfig) != null && r.show ? a(ye, {
          trigger: "hover"
        }, {
          trigger: () => a(ke, {
            size: 20
          }, {
            default: () => [a(ze, null, null)]
          }),
          default: () => {
            var l;
            return (l = e.tooltipConfig) == null ? void 0 : l.text;
          }
        }) : null;
    }, _ = (e) => {
      var r;
      switch (e.type) {
        case "input":
          return a(Be, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "inputNumber":
          return a(Pe, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "radio":
          if (e.valueEnum) {
            let l;
            return e.valueEnum.length > 1 ? a(Ee, s(e.props, {
              value: t[e.key],
              onUpdateValue: (u) => {
                i(u, e.key, e.normalize);
              }
            }), f(l = e.valueEnum.map((u) => a(Y, s(u, {
              key: u.value
            }), null))) ? l : {
              default: () => [l]
            }) : e.valueEnum.map((u) => a(Y, s(e.props, u, {
              key: u.value,
              checked: u.value === t[e.key],
              onUpdateChecked: (n) => {
                z(n, e.key, u.value);
              }
            }), null));
          } else
            return [];
        case "select":
          return a(Fe, s(e.props, {
            options: e.valueEnum,
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "rate":
          return a(be, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "switch":
          return a(Ne, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "timePicker":
          return a(Ue, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            }
          }), null);
        case "datePicker":
          return a(Ce, s(e.props, {
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            },
            value: t[e.key]
          }), null);
        case "colorPicker":
          return a(Se, s(e.props, {
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            },
            value: t[e.key]
          }), null);
        case "checkbox":
          if (e.valueEnum) {
            let l;
            return e.valueEnum.length > 1 ? a(Ve, s(e.props, {
              onUpdateValue: (u) => {
                i(u, e.key, e.normalize);
              },
              value: t[e.key]
            }), f(l = e.valueEnum.map((u) => a(X, s({
              key: u.value
            }, u), null))) ? l : {
              default: () => [l]
            }) : e.valueEnum.map((u) => a(X, s(e.props, {
              key: u.value
            }, u, {
              onUpdateChecked: (n) => {
                i(n, e.key, e.normalize);
              },
              value: t[e.key]
            }), null));
          } else
            return [];
        case "upload":
          return a(ve, s(e.props, {
            fileList: t[e.key] ?? [],
            onUpdateFileList: (l) => {
              i(l, e.key, e.normalize);
            }
          }), {
            default: () => [a(R, null, {
              default: () => [e.buttonText]
            })]
          });
        case "slider":
          return a(we, {
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            },
            value: t[e.key]
          }, null);
        case "autoComplete":
          return a(ge, s(e.props, {
            value: t[e.key],
            onUpdateValue: (l) => {
              i(l, e.key, e.normalize);
            },
            options: (r = e.valueEnum) == null ? void 0 : r.map((l) => {
              const u = t[e.key].value.split(e.splitString)[0];
              return {
                label: u + l,
                value: u + l
              };
            })
          }), null);
        default:
          return;
      }
    }, M = (e) => {
      const {
        autoPlaceholder: r,
        scrollToFirstError: l,
        grid: u
      } = o;
      return e == null ? void 0 : e.map((n) => {
        if (n.type === "divider")
          return a(T, {
            dashed: n.dashed,
            titlePlacement: n.titlePlacement,
            vertical: n.vertical,
            key: n.text
          }, {
            default: () => [n.text]
          });
        if (n.type === "slot")
          return ue(x, n.key);
        if (r) {
          let d;
          (n.type === "input" || n.type === "inputNumber" || n.type === "autoComplete") && (d = `请输入${n.label}`, n.props ? n.props.placeholder = d : n.props = {
            placeholder: d
          }), (n.type === "select" || n.type === "timePicker" || n.type === "datePicker") && (d = `请选择${n.label}`, n.props ? n.props.placeholder = d : n.props = {
            placeholder: d
          });
        }
        if (n.dependencies !== void 0 && n.dependenciesChange !== void 0) {
          const d = n.dependenciesChange;
          if (typeof n.dependencies == "string") {
            const g = c.value[n.dependencies];
            g !== void 0 ? g.add(d) : c.value[n.dependencies] = /* @__PURE__ */ new Set([d]);
          } else
            n.dependencies.forEach((g) => {
              const F = c.value[g];
              F !== void 0 ? F.add(d) : c.value[g] = /* @__PURE__ */ new Set([d]);
            });
        }
        return u ? a(he, {
          key: n.key
        }, {
          default: () => [a(W, s(n.formItemProps, {
            label: n.label,
            path: n.key,
            rule: n.rule
          }), {
            default: () => [l ? a("div", {
              id: `n-pro-form-${n.key}`
            }, null) : null, _(n), q(n)]
          })]
        }) : a(W, s(n.formItemProps, {
          key: n.key,
          label: n.label,
          path: n.key,
          rule: n.rule
        }), {
          default: () => [l ? a("div", {
            id: `n-pro-form-${n.key}`
          }, null) : null, _(n), q(n)]
        });
      });
    }, Z = j(() => {
      const {
        formItems: e,
        steps: r,
        stepsFormItems: l,
        grid: u,
        gridConfig: n
      } = o;
      if (r) {
        let d;
        return u ? a(H, n, f(d = M(l[p.value - 1].formItems)) ? d : {
          default: () => [d]
        }) : M(l[p.value - 1].formItems);
      } else {
        let d;
        return u ? a(H, n, f(d = M(e)) ? d : {
          default: () => [d]
        }) : M(e);
      }
    }), m = j(() => {
      const {
        stepsFormItems: e
      } = o;
      return e.map((r) => a(se, {
        key: r.key,
        title: r.title
      }, null));
    }), ee = j(() => {
      const {
        steps: e,
        stepsFormItems: r
      } = o;
      if (e) {
        const l = r.length;
        return p.value < l ? "下一步" : "提交";
      } else
        return "提交";
    }), ne = j(() => {
      const {
        resetButton: e,
        validateButton: r,
        submitButton: l
      } = o;
      return a(Q, {
        justify: "center"
      }, {
        default: () => [r === !0 ? a(R, {
          onClick: N,
          type: "warning"
        }, {
          default: () => [L("验证")]
        }) : null, e === !0 ? a(R, {
          onClick: b,
          type: "error"
        }, {
          default: () => [L("重置")]
        }) : null, l === !0 ? a(R, {
          onClick: v,
          type: "primary"
        }, {
          default: () => [ee.value]
        }) : null]
      });
    });
    return {
      modalData: t,
      stepsCurrent: p,
      stepsStatus: C,
      spinStatus: y,
      formRef: w,
      handleValidateClick: N,
      handleInputUpdateValue: i,
      handleRadioUpdateChecked: z,
      handleResetClick: b,
      handleSubmitClick: v,
      handleModalShowChange: D,
      handleDrawerShowChange: V,
      Vnode: Z,
      BtnsVnode: ne,
      stepsVnode: m
    };
  },
  render() {
    const {
      steps: o,
      spin: x,
      formProps: P,
      stepsCurrent: t,
      stepsStatus: p,
      spinStatus: C,
      modalData: y,
      title: c,
      Vnode: h,
      BtnsVnode: w,
      stepsVnode: k,
      modal: U,
      modalShow: B,
      modalProps: N,
      drawer: b,
      drawerShow: v,
      drawerProps: i,
      handleModalShowChange: z,
      handleDrawerShowChange: D
    } = this, V = x ? a(ce, {
      show: C
    }, {
      default: () => [a(J, s(P, {
        model: y,
        ref: "formRef"
      }), f(h) ? h : {
        default: () => [h]
      }), w]
    }) : a(K, null, [a(J, s(P, {
      model: y,
      ref: "formRef"
    }), f(h) ? h : {
      default: () => [h]
    }), w]);
    return o ? a(Q, {
      vertical: !0,
      size: 50,
      align: "stretch"
    }, {
      default: () => [a(ie, {
        current: t,
        status: p
      }, f(k) ? k : {
        default: () => [k]
      }), V]
    }) : U ? a(fe, s({
      show: B,
      preset: "card",
      style: {
        width: "50%"
      }
    }, N, {
      onUpdateShow: z
    }), {
      default: () => [c ? a(T, null, f(c) ? c : {
        default: () => [c]
      }) : null, V]
    }) : b ? a(pe, s({
      show: v
    }, i, {
      onUpdateShow: D
    }), {
      default: () => [c ? a(T, null, f(c) ? c : {
        default: () => [c]
      }) : null, V]
    }) : a(K, null, [c ? a(T, null, f(c) ? c : {
      default: () => [c]
    }) : null, V]);
  }
});
export {
  De as default
};
