import { computed as d, defineComponent as x, ref as F, resolveComponent as b, createElementBlock as L, openBlock as w, normalizeClass as R, createVNode as K, normalizeStyle as $, withCtx as B, createBlock as U, createCommentVNode as j } from "vue";
import { A as h, M as E, G as C, Q as O, am as Y, u as y, a as G } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { u as S } from "./menuV2-Da02vc32.js";
import { C as v, h as N } from "./catalog-rNLjUMn-.js";
import { useSnackbar as P, useDialog as z } from "@feedmepos/ui-library";
import { a as T } from "./LinkProductSideSheet-DUHaljek.js";
import { useI18n as Q } from "@feedmepos/mf-common";
function q() {
  const t = S(), o = d(() => t.categorizedRawMenuItems.filter((u) => u.items.length > 0)), i = d(() => {
    const u = t.currentCatalog;
    return u ? v.getItemAvailable(u).map((n) => ({ _id: n })) : [];
  });
  async function c(u) {
    const n = t.currentCatalog;
    if (!n) return;
    const a = new Set(u), r = t.state.menu.modules.item.map((l) => l._id).reduce(
      (l, m) => {
        const f = n.override.items[m];
        return l[m] = N(
          m,
          f,
          a.has(m),
          t.version === h.F_MENU_VERSION.enum.v3
        ), l;
      },
      {}
    ), s = {
      ...n,
      override: {
        ...n.override,
        items: r
      },
      /**
       * linkItemRules and items under override are two ways to manage linked items under catalog,
       * if we changed items under override, we should clear linkItemRules to avoid conflict.
       */
      linkItemRules: null
    };
    await t.updateMenu({
      module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
      operation: E.F_OPERATION_TYPE.update,
      data: s
    });
  }
  async function g(u) {
    await A(u, !0);
  }
  async function p(u) {
    await A(u, !1);
  }
  async function _(u) {
    const n = t.currentCatalog;
    if (!n) return;
    const a = {
      ...n,
      linkItemRules: u
    };
    await t.updateMenu({
      module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
      operation: E.F_OPERATION_TYPE.update,
      data: a
    });
  }
  return {
    linkableItems: o,
    currentItems: i,
    applyProduct: c,
    addProduct: g,
    removeProduct: p,
    saveLinkItemRules: _
  };
}
async function A(t, o) {
  var u;
  const i = S(), c = i.currentCatalog;
  if (!c || t.length === 0) return;
  const g = i.version === h.F_MENU_VERSION.enum.v3, p = t.reduce(
    (n, a) => {
      const e = c.override.items[a];
      return n[a] = N(a, e, o, g), n;
    },
    {}
  ), _ = {
    ...c,
    override: {
      ...c.override,
      items: {
        ...((u = c.override) == null ? void 0 : u.items) || {},
        ...p
      }
    }
  };
  await i.updateMenu({
    module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
    operation: E.F_OPERATION_TYPE.update,
    data: _
  });
}
function H() {
  const t = S(), o = P(), i = z(), c = C(o), g = O(c), p = d(() => {
    const n = t.categorizedRawMenuItems, a = new Set(
      t.currentCatalog ? v.getItemAvailable(t.currentCatalog) : t.state.menu.modules.item.map((e) => e._id)
    );
    return n.map((e) => ({
      ...e,
      items: e.items.filter((r) => a.has(r._id))
    })).filter((e) => e.items.length > 0);
  }), _ = d(() => {
    var r;
    const n = t.currentScheduler;
    if (!n) return [];
    const a = t.currentCatalog, e = a ? ((r = a.override.scheduler[n._id]) == null ? void 0 : r.items) ?? [] : n.items;
    return [
      ...new Set(e.filter((s) => s.orderFrom != h.F_ORDER_FROM.enum.NONE).map((s) => s._id))
    ].map((s) => ({ _id: s }));
  });
  async function u(n) {
    const a = t.currentScheduler;
    if (!a) return;
    const e = t.currentCatalog;
    if (e && t.rules.scheduler.override) {
      const r = new v(
        e,
        t,
        g.updateMenu,
        i
      );
      r.overrideCatalogSchedulerItems(n, a._id), await t.updateMenu({
        module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
        operation: E.F_OPERATION_TYPE.update,
        data: r.getCatalog()
      });
    } else {
      const r = n.reduce(
        (l, m) => {
          const f = a.items.filter((I) => I._id == m) ?? [];
          return l = l.concat(
            Y(m, f, (I) => ({
              ...I,
              orderFrom: null
            }))
          ), l;
        },
        []
      ), s = {
        ...a,
        items: r
      };
      await t.updateMenu({
        module: h.F_MENU_MODULE_V4_KEY.enum.scheduler,
        operation: E.F_OPERATION_TYPE.update,
        data: s
      });
    }
  }
  return {
    linkableItems: p,
    currentItems: _,
    applyProduct: u
  };
}
const J = () => {
  const t = y(), o = P(), i = C(o), c = O(i), g = d(() => t.categorizedRawMenuItems.filter((n) => n.items.length > 0)), p = d(() => {
    const n = t.currentCatalog;
    return n ? v.getItemAvailable(n).map((a) => ({ _id: a })) : [];
  });
  async function _(n) {
    const a = t.currentCatalog;
    if (!a) return;
    const e = new Set(n), s = t.state.menu.modules.item.map((m) => m._id).reduce(
      (m, f) => {
        const I = a.override.items[f];
        return m[f] = N(
          f,
          I,
          e.has(f),
          t.version === G.F_MENU_VERSION.v3
        ), m;
      },
      {}
    ), l = {
      ...a,
      override: { ...a.override, items: s },
      /**
       * linkItemRules and items under override are two ways to manage linked items under catalog,
       * if we changed items under override, we should clear linkItemRules to avoid conflict.
       */
      linkItemRules: null
    };
    await c.updateMenu(
      {
        module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
        operation: E.F_OPERATION_TYPE.update,
        data: l
      },
      l.name
    );
  }
  async function u(n) {
    const a = t.currentCatalog;
    if (!a) return;
    const e = {
      ...a,
      linkItemRules: n
    };
    await c.updateMenu(
      {
        module: h.F_MENU_MODULE_V4_KEY.enum.catalog,
        operation: E.F_OPERATION_TYPE.update,
        data: e
      },
      e.name
    );
  }
  return {
    linkableItems: g,
    currentItems: p,
    applyProduct: _,
    saveLinkItemRules: u
  };
}, W = () => {
  const t = y(), o = P(), i = z(), c = C(o), g = O(c), p = d(() => {
    const n = t.categorizedRawMenuItems, a = new Set(
      t.currentCatalog ? v.getItemAvailable(t.currentCatalog) : t.state.menu.modules.item.map((e) => e._id)
    );
    return n.map((e) => ({
      ...e,
      items: e.items.filter((r) => a.has(r._id))
    })).filter((e) => e.items.length > 0);
  }), _ = d(() => {
    var r;
    const n = t.currentScheduler;
    if (!n) return [];
    const a = t.currentCatalog, e = a ? ((r = a.override.scheduler[n._id]) == null ? void 0 : r.items) ?? [] : n.items;
    return [
      ...new Set(e.filter((s) => s.orderFrom != h.F_ORDER_FROM.enum.NONE).map((s) => s._id))
    ].map((s) => ({ _id: s }));
  });
  async function u(n) {
    const a = t.currentScheduler;
    if (!a) return;
    const e = t.currentCatalog;
    if (e && t.rules.scheduler.override) {
      const r = new v(e, t, g.updateMenu, i);
      r.overrideCatalogSchedulerItems(n, a._id), await r.update();
    } else {
      const r = n.reduce(
        (l, m) => {
          const f = a.items.filter((I) => I._id == m) ?? [];
          return l = l.concat(
            Y(m, f, (I) => ({
              ...I,
              orderFrom: null
            }))
          ), l;
        },
        []
      ), s = {
        ...a,
        items: r
      };
      await g.updateMenu(
        {
          module: h.F_MENU_MODULE_V4_KEY.enum.scheduler,
          operation: E.F_OPERATION_TYPE.update,
          data: s
        },
        s.name
      );
    }
  }
  return {
    linkableItems: p,
    currentItems: _,
    applyProduct: u
  };
};
function se(t) {
  const o = {
    // Under FdoMenuV4Category _id is string, but here we need null for Addon, so we bypass the type checking
    // @ts-ignore
    _id: null,
    name: "Addon",
    position: 0
  };
  if (!t) {
    const e = y(), r = d(() => e.state.menu.modules.item ?? []), s = d(() => {
      var M;
      return ((M = e.currentCatalog) == null ? void 0 : M.linkItemRules) ?? null;
    }), l = W(), m = J(), f = d(() => !e.state.currentSchedulerId), I = (M) => {
      if (M.mode === T.Rules)
        m.saveLinkItemRules(M.linkItemRules);
      else {
        const k = M.items.map((V) => V._id);
        f.value ? m.applyProduct(k) : l.applyProduct(k);
      }
    }, D = d(() => [o, ...e.state.menu.modules.category ?? []]);
    return {
      menuItems: r,
      categories: D,
      linkItemRules: s,
      catalogManager: m,
      schedulerManager: l,
      noScheduler: f,
      handleSubmit: I
    };
  }
  const i = S(), c = d(() => i.state.menu.modules.item ?? []), g = d(() => {
    var e;
    return ((e = i.currentCatalog) == null ? void 0 : e.linkItemRules) ?? null;
  }), p = H(), _ = q(), u = d(() => !i.state.currentSchedulerId), n = (e) => {
    if (e.mode === T.Rules)
      _.saveLinkItemRules(e.linkItemRules);
    else {
      const r = e.items.map((s) => s._id);
      u.value ? _.applyProduct(r) : p.applyProduct(r);
    }
  }, a = d(() => [o, ...i.state.menu.modules.category ?? []]);
  return {
    menuItems: c,
    categories: a,
    linkItemRules: g,
    catalogManager: _,
    schedulerManager: p,
    noScheduler: u,
    handleSubmit: n
  };
}
const X = ["alt", "src", "crossorigin"], ue = /* @__PURE__ */ x({
  __name: "Thumbnail",
  props: {
    src: {},
    alt: {},
    className: {},
    size: {},
    crossorigin: { type: Boolean, default: !0 }
  },
  setup(t) {
    const o = t, { t: i } = Q(), c = F(!1), g = F(!o.src), p = () => {
      c.value = !1, g.value = !0;
    }, _ = () => {
      c.value = !0, g.value = !0;
    }, u = d(() => o.alt ? i("menu.thumbnail.alt.with_name", { name: o.alt }) : i("menu.thumbnail.alt.default")), n = d(() => typeof o.size == "number" ? {
      width: o.size,
      height: o.size
    } : {
      width: o.size.width,
      height: o.size.height
    }), a = d(() => {
      if (!o.src) return null;
      if (o.crossorigin) {
        const e = o.src.includes("?") ? "&" : "?";
        return `${o.src}${e}_cors=${Date.now()}`;
      }
      return o.src;
    });
    return (e, r) => {
      const s = b("fm-icon"), l = b("fm-card");
      return w(), L("div", {
        class: R([e.className, "flex items-center justify-center"])
      }, [
        K(l, {
          class: R(["flex items-center justify-center", "rounded-md"]),
          style: $({ width: `${n.value.width}px`, height: `${n.value.height}px` }),
          variant: "outlined"
        }, {
          default: B(() => [
            a.value && !c.value ? (w(), L("img", {
              key: 0,
              alt: u.value,
              class: R(["w-full h-full", "object-cover", g.value ? "" : "hidden"]),
              src: a.value,
              crossorigin: o.crossorigin ? "anonymous" : void 0,
              onError: _,
              onLoad: p
            }, null, 42, X)) : (w(), U(s, {
              key: 1,
              color: "neutral-gray-300",
              name: "image",
              outline: ""
            })),
            g.value ? j("", !0) : (w(), U(s, {
              key: 2,
              class: "animate-spin",
              name: "progress_activity",
              spin: ""
            }))
          ]),
          _: 1
        }, 8, ["style"])
      ], 2);
    };
  }
});
export {
  ue as _,
  q as a,
  se as u
};
