import { defineComponent as H, computed as l, ref as V, createVNode as F, watch as J, openBlock as X, createBlock as Z, unref as f, mergeProps as ee, withCtx as te, createElementVNode as ae, normalizeClass as ie } from "vue";
import { isValid as oe } from "@aplus-frontend/utils";
import { useInjectForm as re } from "../../ap-form/context.mjs";
import { useInjectApTable as se } from "../../ap-table/context.mjs";
import "../../config-provider/index.mjs";
import { ApBatchActionGroup as le } from "../ap-batch-action-group/index.mjs";
import "./components/main-button-content.vue.mjs";
import "./components/menu-list-content.vue.mjs";
import { useSendToUrl as ne } from "./hooks/use-send-to-url.mjs";
import { useViewProvide as me } from "./hooks/use-view-provide.mjs";
import ue from "./icons/line-down.vue.mjs";
import ce from "./style/index.mjs";
import { SYSTEM_VIEW_KEY as v } from "./utils/enum.mjs";
import { useGlobalConfig as N } from "../../config-provider/hooks/use-global-config.mjs";
import { useNamespace as fe } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as ve } from "../../config-provider/hooks/use-locale.mjs";
import pe from "./components/main-button-content.vue2.mjs";
import we from "./components/menu-list-content.vue2.mjs";
const Pe = /* @__PURE__ */ H({
  name: "ApView",
  __name: "ap-view",
  props: {
    tableKey: {},
    saveDataSource: {},
    viewCacheOption: {},
    maxViewNum: {
      default: 20
    },
    isDefaultSystemView: {
      type: Boolean,
      default: !1
    },
    isFirstDefaultSystemView: {
      type: Boolean,
      default: !1
    },
    manual: {
      type: Boolean,
      default: !1
    },
    emptyData: {},
    extraParams: {},
    sendToUrl: {
      type: Boolean,
      default: !0
    },
    initToUrl: {
      type: Boolean,
      default: !0
    },
    extraUrlParams: {},
    apViewQuerySync: {}
  },
  setup(_, {
    expose: O
  }) {
    const a = _, P = N("viewCacheOption"), B = N("apView"), {
      b: S
    } = fe("ap-view"), {
      t: L
    } = ve(), C = ce("ap-view"), h = l(() => ({
      viewKey: v,
      viewName: L("ap.apView.systemView")
    })), {
      getSearchFormValues: A,
      setSearchFormValues: E,
      submit: M,
      reset: j,
      submitWith: k,
      getSearchFormSorterItems: Q,
      setSearchFormSorterItems: W,
      resetSearchFormSorterItems: G
    } = se(), {
      model: $
    } = re(), Y = V(!1), K = V(!1), w = V("");
    let m = !1, U = !1, b = !0;
    const z = {
      dropdownType: "dropdown",
      content: F(pe, {
        maxViewNum: a.maxViewNum
      }, null)
    }, o = V([h.value]), R = l(() => o.value.map((t) => ({
      id: t.viewKey,
      content: F(we, {
        view: t
      }, null)
    }))), r = {
      getSearchFormValues: A,
      setSearchFormValues: E,
      submit: M,
      reset: j,
      submitWith: k,
      getSearchFormSorterItems: Q,
      setSearchFormSorterItems: W,
      resetSearchFormSorterItems: G,
      ...a.saveDataSource
    }, y = {
      ...P.value,
      ...a.viewCacheOption
    }, {
      mountedData: D,
      isValidMountedData: T,
      isFirstSendToUrl: d
    } = ne({
      saveDataSource: r,
      sendToUrl: l(() => a.sendToUrl),
      tableKey: l(() => a.tableKey),
      searchFormModel: l(() => a?.saveDataSource?.searchFormModel || $?.value),
      extraUrlParams: l(() => a.extraUrlParams),
      urlQueryAdapter: l(() => a.apViewQuerySync || B.value?.apViewQuerySync)
    }), I = () => a.isDefaultSystemView ? v : (a.isFirstDefaultSystemView || U) && m ? v : o.value.find((i) => i.isDefault)?.viewKey ?? v, u = (t) => {
      w.value = t;
      const i = o.value.find((e) => e.viewKey === w.value);
      i && setTimeout(() => {
        i.viewKey === v ? m ? a.manual || (a.sendToUrl && b && a.initToUrl && T.value && d.value && (r?.setSearchFormValues?.(D.value), d.value = !1), r?.submitWith?.()) : a.manual || (r?.resetSearchFormSorterItems?.(), r?.reset?.()) : (a.sendToUrl && b && a.initToUrl && T.value && m && d.value ? (r?.setSearchFormValues?.(D.value), d.value = !1) : r?.setSearchFormValues?.({
          ...Object.fromEntries(Object.keys(r?.getSearchFormValues?.() ?? {}).map((e) => [e, void 0])) ?? {},
          ...a?.emptyData ?? {},
          ...[...r?.getSearchFormSorterItems?.() ?? [], ...a?.extraParams?.map((e) => ({
            name: e
          })) ?? []].reduce((e, s) => (i.viewConfig?.searchForm?.[s.name] && (e[s.name] = i.viewConfig?.searchForm?.[s.name]), e), {}) ?? {}
        }), r?.setSearchFormSorterItems?.(i.viewConfig?.sorterItems ?? []), m ? a.manual || r?.submitWith?.() : r?.submit?.()), m = !1;
      });
    }, x = {
      getViewList: async (t) => {
        try {
          const {
            tableKey: i
          } = t, e = await y?.getViewList?.({
            tableKey: i
          });
          return o.value = [h.value, ...e ?? []], u(I()), o.value;
        } catch {
          return o.value = [h.value], u(v), o.value;
        }
      },
      addView: async (t) => {
        const {
          tableKey: i,
          viewKey: e,
          viewName: s,
          isDefault: n,
          viewConfig: p
        } = t;
        await y?.addView?.({
          tableKey: i,
          viewKey: e,
          viewName: s,
          isDefault: n,
          viewConfig: p
        }), n && (o.value = o.value.map((c) => ({
          ...c,
          isDefault: !1
        }))), o.value = [...o.value, {
          viewKey: e,
          viewName: s,
          isDefault: n,
          viewConfig: p
        }], u(e);
      },
      changeView: async (t) => {
        const {
          tableKey: i,
          viewKey: e,
          viewName: s,
          isDefault: n,
          viewConfig: p
        } = t;
        await y?.changeView?.({
          tableKey: i,
          viewKey: e,
          viewName: s,
          isDefault: n,
          viewConfig: p
        }), n && (o.value = o.value.map((g) => ({
          ...g,
          isDefault: !1
        })));
        const c = o.value.find((g) => g.viewKey === e);
        c && (c.viewName = s, c.isDefault = n, c.viewConfig = p), u(e);
      },
      removeView: async (t) => {
        const {
          tableKey: i,
          viewKey: e
        } = t;
        await y?.removeView?.({
          tableKey: i,
          viewKey: e
        }), o.value = o.value.filter((s) => s.viewKey !== e), e === w.value && u(I());
      }
    }, q = {
      tableKey: l(() => a.tableKey),
      currentViewKey: w,
      changeCurrentViewKey: u,
      popoverOpen: Y,
      menuOpen: K,
      viewList: o,
      viewListCRUD: x,
      saveDataSource: r
    };
    return me(q), J(() => a.tableKey, (t) => {
      oe(t) && (m = !0, x?.getViewList?.({
        tableKey: t
      }));
    }, {
      immediate: !0
    }), O({
      setFirstDefaultSystemView: (t) => {
        U = t;
      },
      setInitToUrl: (t) => {
        b = t;
      },
      getMountedData: () => D.value
    }), (t, i) => (X(), Z(f(le), ee({
      ...t.$attrs
    }, {
      open: K.value,
      "onUpdate:open": i[0] || (i[0] = (e) => K.value = e),
      class: [f(S)(), f(C)],
      "button-props": z,
      "menu-list": R.value,
      "overlay-class-name": [f(S)("overlay"), f(C)],
      trigger: "click",
      placement: "bottom"
    }), {
      icon: te(() => [ae("div", {
        class: ie([f(S)("icon")])
      }, [F(ue)], 2)]),
      _: 1
    }, 16, ["open", "class", "menu-list", "overlay-class-name"]));
  }
});
export {
  Pe as default
};
