import { defineComponent as T, ref as z, computed as $, watch as K, openBlock as L, createBlock as Y, unref as e, withCtx as t, createElementVNode as u, normalizeClass as r, createVNode as a, createSlots as E, toDisplayString as s, createTextVNode as d } from "vue";
import { Popover as R, Tooltip as U, Space as _, Checkbox as j } from "@aplus-frontend/antdv";
import q from "dayjs";
import { cloneDeep as g } from "lodash-unified";
import "../../../ap-button/index.mjs";
import { ApField as B } from "../../../ap-field/index.mjs";
import "../../../ap-form/ap-form.vue.mjs";
import "../../../config-provider/index.mjs";
import { useViewInject as M } from "../hooks/use-view-provide.mjs";
import O from "../icons/line-eye.vue.mjs";
import A from "../style/index.mjs";
import { generateUniqueKey as P } from "../utils/uuid.mjs";
import { useLocale as G } from "../../../config-provider/hooks/use-locale.mjs";
import { useNamespace as H } from "../../../config-provider/hooks/use-namespace.mjs";
import w from "../../../ap-form/ap-form.vue2.mjs";
import x from "../../../ap-button/ap-button.vue.mjs";
const ce = /* @__PURE__ */ T({
  __name: "main-button-content",
  props: {
    maxViewNum: {}
  },
  setup(N) {
    const f = N, { t: o } = G(), { b: i } = H("ap-view"), m = A("ap-view"), V = z(), { tableKey: b, popoverOpen: p, viewList: c, saveDataSource: h, viewListCRUD: F } = M(), y = $({
      get: () => p.value && c.value.length < f.maxViewNum + 1,
      set: (n) => {
        c.value.length < f.maxViewNum + 1 && (p.value = n);
      }
    });
    K(p, (n) => {
      n && (V.value?.resetFields(), setTimeout(() => {
        V.value?.setFieldsValue({
          isDefault: !c.value.some((l) => l.isDefault)
        });
      }));
    });
    const D = async (n) => {
      const { viewName: l, isDefault: v } = n, S = P(), k = g(
        h?.getSearchFormValues?.(
          !!h?.searchFormTransform
        )
      ), C = g(h?.getSearchFormSorterItems?.()), I = {
        searchForm: k,
        sorterItems: C
      };
      await F.addView?.({
        tableKey: b.value,
        viewKey: S,
        viewName: l,
        isDefault: v,
        viewConfig: I
      }), p.value = !1;
    };
    return (n, l) => (L(), Y(e(R), {
      open: y.value,
      "onUpdate:open": l[1] || (l[1] = (v) => y.value = v),
      placement: "bottom",
      trigger: "click",
      arrow: !1,
      "overlay-inner-style": {
        padding: "12px 16px"
      }
    }, {
      content: t(() => [
        u("div", {
          class: r([e(i)(), e(i)("main-content-popover"), e(m)])
        }, [
          a(w, {
            ref_key: "formRef",
            ref: V,
            layout: "vertical",
            "initial-values": {
              viewName: `${e(o)("ap.apView.newView")} ${e(q)().format("YYYY-MM-DD").toString()}`
            },
            onFinish: D
          }, {
            default: t(() => [
              u("div", {
                class: r([e(i)("main-content-popover-title-content"), e(m)])
              }, [
                u("div", {
                  class: r([e(i)("main-content-popover-title-content-title")])
                }, s(e(o)("ap.apView.saveNewView")), 3),
                a(w.FormItem, {
                  name: "isDefault",
                  "value-prop-name": "checked"
                }, {
                  default: t(() => [
                    a(e(j), null, {
                      default: t(() => [
                        d(s(e(o)("ap.apView.setDefault")), 1)
                      ]),
                      _: 1
                    })
                  ]),
                  _: 1
                })
              ], 2),
              a(w.FormItem, {
                label: e(o)("ap.apView.viewName"),
                name: "viewName",
                rules: [{ required: !0, message: e(o)("ap.apView.notEmpty") }]
              }, {
                default: t(() => [
                  a(e(B).Text, {
                    placeholder: e(o)("ap.apView.viewName"),
                    maxlength: 20,
                    "show-count": ""
                  }, null, 8, ["placeholder"])
                ]),
                _: 1
              }, 8, ["label", "rules"]),
              a(w.FormItem, {
                class: r([e(i)("main-content-popover-btn"), e(m)])
              }, {
                default: t(() => [
                  a(e(_), { size: 8 }, {
                    default: t(() => [
                      a(e(x), {
                        size: "small",
                        onClick: l[0] || (l[0] = (v) => p.value = !1)
                      }, {
                        default: t(() => [
                          d(s(e(o)("ap.apView.cancel")), 1)
                        ]),
                        _: 1
                      }),
                      a(e(x), {
                        type: "primary",
                        size: "small",
                        "html-type": "submit"
                      }, {
                        default: t(() => [
                          d(s(e(o)("ap.apView.save")), 1)
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }, 8, ["class"])
            ]),
            _: 1
          }, 8, ["initial-values"])
        ], 2)
      ]),
      default: t(() => [
        u("div", {
          class: r({
            [e(i)("main-content")]: !0,
            [e(i)("main-content-disabled")]: e(c).length >= f.maxViewNum + 1,
            hashId: e(m)
          })
        }, [
          a(e(U), { placement: "bottom" }, E({
            default: t(() => [
              a(e(_), { size: 4 }, {
                default: t(() => [
                  a(O, {
                    class: r([e(i)("icon"), e(m)])
                  }, null, 8, ["class"]),
                  u("div", null, s(e(o)("ap.apView.saveView")), 1)
                ]),
                _: 1
              })
            ]),
            _: 2
          }, [
            e(c).length >= f.maxViewNum + 1 ? {
              name: "title",
              fn: t(() => [
                d(s(e(o)("ap.apView.maxTip")), 1)
              ]),
              key: "0"
            } : void 0
          ]), 1024)
        ], 2)
      ]),
      _: 1
    }, 8, ["open"]));
  }
});
export {
  ce as default
};
