var We = Object.defineProperty;
var qe = (e, t, n) => t in e ? We(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
var ne = (e, t, n) => qe(e, typeof t != "symbol" ? t + "" : t, n);
import { K as ae, I as C, D as T, A as ue, aq as re, M as G, J as ie, ae as x, a as q, $ as V, ao as oe, ar as Le, as as ze, B as Ge, ag as Be, at as He, au as Je, ad as Ye } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { defineStore as Qe } from "pinia";
import { shallowRef as N, ref as B, computed as S, shallowReactive as Xe, effectScope as Ze, watch as et } from "vue";
import { useCoreStore as se } from "@feedmepos/mf-common";
import { useSnackbar as tt } from "@feedmepos/ui-library";
const nt = "/assets/linked-status-D6bN0y-3.js", rt = "/assets/override-menu-qTFPk1y0.js";
function ot(e) {
  var o;
  const t = C(e), n = (o = t.variant) == null ? void 0 : o.variantCombinations;
  return delete t.variant, delete t.thumbnail, [t, ...n || []];
}
function st(e, t, n, o) {
  const r = C(t), u = n ? r.modules.catalog.find((v) => v._id === n) : null, l = o ? r.modules.scheduler.find((v) => v._id === o) : null, a = u ? C(u) : void 0, c = n ? r.modules.catalog.filter((v) => v._id !== n) : [], i = l ? C(l) : void 0, f = e.reduce((v, y) => (v[y._id] = ot(y), v), {});
  function h(v = []) {
    const y = e.map((p) => p._id);
    return [
      ...v.filter((p) => !y.includes(p._id)),
      ...Object.values(f).flatMap((p) => p)
    ];
  }
  return a && o ? (a.override.scheduler = {
    ...a.override.scheduler,
    [o]: {
      ...a.override.scheduler[o],
      _id: o,
      items: h(i == null ? void 0 : i.items).map((v) => ({
        ...v,
        orderFrom: ue.F_ORDER_FROM.enum.ALL
      }))
    }
  }, r.modules.catalog = [...c, a], r) : a ? (a.override.items = {
    ...a.override.items,
    ...f
  }, r.modules.catalog = [...c, a], r) : i ? (i.items = h(i.items), r.modules.scheduler = [
    ...r.modules.scheduler.filter((v) => v._id !== o),
    i
  ], r) : {
    ...re(),
    modules: {
      ...re().modules,
      catalog: r.modules.catalog,
      item: r.modules.item,
      group: r.modules.group,
      scheduler: r.modules.scheduler
    },
    _id: r._id
  };
}
function $(e, t, n) {
  var u;
  if (n >= 3) return e;
  const o = t.modules.item.find((l) => l._id === e._id);
  if (!o)
    throw new Error(`_insertItemOverriders - Item not found - ${e._id}`);
  const r = (u = o.groups) == null ? void 0 : u.map((l) => {
    var i, f, h;
    const a = (i = e == null ? void 0 : e.groups) == null ? void 0 : i.find((v) => v._id === l._id);
    let c;
    return a ? c = ((f = l.items) == null ? void 0 : f.map((v) => {
      var p;
      const y = (p = a.items) == null ? void 0 : p.find(
        (b) => b._id === v._id && b.combinationKey === v.combinationKey
      );
      return $(y || v, t, n + 1);
    })) ?? [] : c = ((h = l.items) == null ? void 0 : h.map((v) => $(v, t, n + 1))) ?? [], {
      ...l,
      items: c
    };
  });
  return {
    _id: o._id,
    name: e.name || o.name,
    code: e.code || o.code,
    combinationKey: e.combinationKey,
    groups: r
  };
}
function at(e, t) {
  const n = e.groups.map((u) => {
    const l = t.modules.group.find((c) => c._id === u._id), a = ((l == null ? void 0 : l.items) || []).map((c) => {
      var h;
      const i = (h = u.items) == null ? void 0 : h.find(
        (v) => v._id === c._id && v.combinationKey === c.combinationKey
      );
      if (i)
        return $(i, t, 1);
      const f = {
        ...T.generator.initMenuV4ItemOverrider(),
        _id: c._id,
        combinationKey: c.combinationKey
      };
      return $(f, t, 1);
    });
    return { ...u, items: a };
  }), o = e.variant ? e.variant.variantCombinations.map((u) => $(u, t, 1)) : [], r = {
    ...T.generator.initMenuV4ItemOverrider(),
    _id: e._id,
    name: e.name,
    code: e.code,
    groups: n
  };
  return [...o, r];
}
function H(e, t) {
  var o;
  if (!t)
    return e;
  const n = { ...e, code: t.code };
  return n.groups = (o = n.groups) == null ? void 0 : o.map((r) => {
    var l, a;
    const u = (l = t.groups) == null ? void 0 : l.find((c) => c._id === r._id);
    return r.items = (a = r.items) == null ? void 0 : a.map((c) => {
      var f;
      const i = (f = u == null ? void 0 : u.items) == null ? void 0 : f.find(
        (h) => h._id === c._id && h.combinationKey === c.combinationKey
      );
      return H(c, i);
    }), r;
  }), n;
}
function ut(e, t) {
  const n = e.items[t._id];
  t.variant && (t.variant = {
    ...t.variant,
    variantCombinations: t.variant.variantCombinations.map((r) => {
      const u = n.find(
        (l) => l._id === r._id && l.combinationKey === r.combinationKey
      );
      if (!u)
        throw new Error(`Item overrider not found - ${r._id} - ${r.combinationKey}`);
      return H(u, r);
    })
  });
  const o = n.find((r) => !r.combinationKey);
  if (!o)
    throw new Error(`Base item overrider not found - ${t._id}`);
  return t.groups = (o.groups || []).map((r) => {
    var l;
    const u = t.groups.find((a) => a._id === r._id);
    return r.items = (l = r.items) == null ? void 0 : l.map((a) => {
      var i;
      const c = (i = u == null ? void 0 : u.items) == null ? void 0 : i.find(
        (f) => f._id === a._id && f.combinationKey === a.combinationKey
      );
      return H(a, c);
    }), r;
  }), {
    ...t,
    price: (o == null ? void 0 : o.price) || t.price
  };
}
async function Dt(e, t, n, o, r, u = !0) {
  const l = C(n), a = u ? st(t, l, o, r) : l, c = t.reduce((h, v) => (h[v._id] = a.modules.item.find((y) => y._id === v._id), h), {}), i = t.reduce((h, v) => {
    const y = v._id;
    return h[y] = at(c[y], e), h;
  }, {}), f = T.getFallbackPrices({
    rawMenu: a,
    items: i,
    catalogId: o || void 0,
    schedulerId: r
  });
  return t.map((h) => {
    const v = h._id, y = c[v];
    return {
      ...ut(f, y),
      category: o || r ? y == null ? void 0 : y.category : null
    };
  });
}
function Vt(e, t) {
  if (e.length === 0) return "";
  const n = e == null ? void 0 : e.split("variant");
  return ((n == null ? void 0 : n.reduce((r, u) => {
    if (u && u.length > 0) {
      const l = `variant${u.split("/")[0]}`, a = u.split("/")[1], i = t.find(
        ({ _id: f }) => f === l
      ).options.find(({ _id: f }) => f === a);
      r.push(((i == null ? void 0 : i.name) || "").trim());
    }
    return r;
  }, [])) || []).join(" / ");
}
const L = /* @__PURE__ */ new Map();
async function Tt(e, t) {
  const n = `${e._id}-${e.updatedAt}-${t}`;
  if (L.has(n))
    return L.get(n);
  const o = await ae(
    nt,
    JSON.stringify({
      menu: e,
      itemId: t
    })
  );
  return L.set(n, o), o;
}
async function ce(e, t, n) {
  return await ae(rt, {
    menu: e,
    catalogId: t,
    schedulerId: n
  });
}
function Ft(e, t, n) {
  const o = /* @__PURE__ */ new Set();
  function r(u, l) {
    if (!e || !u || !l || o.has(l)) return !1;
    o.add(l);
    const a = e.modules.item.find((c) => c._id === l);
    return !a || !a.groups ? !1 : a.groups.some((c) => c._id === u) ? !0 : a.groups.some((c) => {
      const i = e.modules.group.find((f) => f._id === c._id);
      for (const f of (i == null ? void 0 : i.items) || [])
        if (r(u, f._id)) return !0;
      return !1;
    });
  }
  return r(t, n);
}
function Kt(e, t, n) {
  const o = /* @__PURE__ */ new Set();
  function r(u, l) {
    if (!e || !u || !l || o.has(u)) return !1;
    o.add(u);
    const a = e.modules.group.find((c) => c._id === u);
    return !a || !a.items ? !1 : a.items.some((c) => c._id === l) ? !0 : a.items.some((c) => {
      const i = e.modules.item.find((f) => f._id === c._id);
      for (const f of (i == null ? void 0 : i.groups) || [])
        if (r(f._id, l)) return !0;
      return !1;
    });
  }
  return r(t, n);
}
const z = /* @__PURE__ */ new Map();
async function it(e, t, n) {
  const o = `${e._id}-${e.updatedAt}-${t || "null"}-${n || "null"}`;
  if (z.has(o))
    return z.get(o);
  const r = await ce(e, t, n);
  return z.set(o, r), r;
}
async function Pt(e, t, n) {
  var l, a;
  const o = await it(e, t, null), r = [], u = (c, i) => {
    const f = C(c);
    for (const h in i)
      i[h] && (f[h] = i[h]);
    return f;
  };
  for (const c of o.modules.group) {
    const i = (l = c.items) == null ? void 0 : l.find((h) => h._id === n && !h.combinationKey), f = e.modules.item.find((h) => h._id === n);
    if (i) {
      const h = (a = c.items) == null ? void 0 : a.filter((y) => y._id === n && y.combinationKey), v = [i, ...h].map((y) => {
        var w, k;
        const p = y.combinationKey ? (k = (w = f == null ? void 0 : f.variant) == null ? void 0 : w.variantCombinations) == null ? void 0 : k.find(
          (I) => I.combinationKey === y.combinationKey
        ) : f, b = p && f ? u(f, p) : y;
        return u(b, y);
      });
      r.push({
        group: c,
        items: v
      });
    }
  }
  return r;
}
const M = 0, F = 3;
function ct(e) {
  const t = /^\/(.*)\/([gimyu]*)$/.exec(e.toString());
  if (!t)
    throw new Error("Invalid RegExp");
  return new RegExp(t[1] ?? "", t[2]);
}
function O(e) {
  if (typeof e != "object")
    return e;
  if (e === null)
    return null;
  if (Array.isArray(e))
    return e.map(O);
  if (e instanceof Date)
    return new Date(e.getTime());
  if (e instanceof RegExp)
    return ct(e);
  const t = {};
  for (const n in e)
    Object.prototype.hasOwnProperty.call(e, n) && (t[n] = O(e[n]));
  return t;
}
function P(e) {
  return e instanceof RegExp ? e.toString() : e;
}
function E(e) {
  return typeof e == "object" && e !== null && !Array.isArray(e) && !(e instanceof Date) && !(e instanceof RegExp);
}
function K(e, t) {
  if (e === t) return !0;
  if (typeof e != typeof t) return !1;
  if (e instanceof Date && t instanceof Date)
    return e.getTime() === t.getTime();
  if (e instanceof RegExp && t instanceof RegExp)
    return e.toString() === t.toString();
  if (Array.isArray(e) && Array.isArray(t)) {
    if (e.length !== t.length) return !1;
    for (let n = 0; n < e.length; n++)
      if (!K(e[n], t[n])) return !1;
    return !0;
  }
  if (E(e) && E(t)) {
    const n = Object.keys(e), o = Object.keys(t);
    if (n.length !== o.length) return !1;
    for (const r of n)
      if (!Object.prototype.hasOwnProperty.call(t, r) || !K(e[r], t[r])) return !1;
    return !0;
  }
  return !1;
}
function lt(e, t, n) {
  const o = e.length, r = t.length, u = /* @__PURE__ */ new Map();
  for (let c = 0; c < r; c++) {
    const i = n ? n(t[c]) : JSON.stringify(t[c]);
    u.has(i) || u.set(i, []), u.get(i).push(c);
  }
  const l = [], a = /* @__PURE__ */ new Set();
  for (let c = 0; c < o; c++) {
    const i = e[c], f = n ? n(i) : JSON.stringify(i), h = u.get(f);
    if (h) {
      for (const v of h)
        if (!a.has(v) && K(i, t[v])) {
          l.push({ leftIndex: c, rightIndex: v }), a.add(v);
          break;
        }
    }
  }
  return l;
}
function dt(e, t, n) {
  var v, y;
  if (K(e, t)) return;
  const o = { _t: "a" }, r = ((v = n == null ? void 0 : n.arrays) == null ? void 0 : v.detectMove) !== !1, u = ((y = n == null ? void 0 : n.arrays) == null ? void 0 : y.includeValueOnMove) === !0, l = n == null ? void 0 : n.objectHash;
  let a = r ? lt(e, t, l) : [];
  if (!l && e.length === t.length && e.length > 0 && a.length < e.length) {
    const p = E(e[0]) || Array.isArray(e[0]), b = a.length > 0;
    if (p || b) {
      a = [];
      for (let w = 0; w < e.length; w++)
        a.push({ leftIndex: w, rightIndex: w });
    }
  }
  new Set(a.map((p) => p.leftIndex));
  const c = new Set(a.map((p) => p.rightIndex)), i = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map();
  for (const p of a)
    i.set(p.leftIndex, p.rightIndex), f.set(p.rightIndex, p.leftIndex);
  for (let p = 0; p < e.length; p++) {
    const b = i.get(p);
    if (b !== void 0) {
      const w = J(e[p], t[b], n);
      if (w !== void 0 && (o[b] = w), r && b !== p) {
        const k = `_${p}`, I = u ? [e[p], b, F] : ["", b, F];
        o[k] = I;
      }
    } else {
      const w = `_${p}`;
      o[w] = [e[p], M, M];
    }
  }
  for (let p = 0; p < t.length; p++)
    c.has(p) || (o[p] = [t[p]]);
  const h = Object.keys(o);
  if (!(h.length === 1 && h[0] === "_t"))
    return o;
}
function ft(e, t, n) {
  if (K(e, t)) return;
  const o = {}, r = Object.keys(e), u = Object.keys(t), l = /* @__PURE__ */ new Set([...r, ...u]);
  for (const a of l) {
    if (n != null && n.propertyFilter && !n.propertyFilter(a, { left: e, right: t }))
      continue;
    const c = Object.prototype.hasOwnProperty.call(e, a), i = Object.prototype.hasOwnProperty.call(t, a);
    if (c && i) {
      const f = J(e[a], t[a], n);
      f !== void 0 && (o[a] = f);
    } else if (c && !i) {
      const f = n != null && n.omitRemovedValues ? 0 : e[a];
      o[a] = [f, M, M];
    } else !c && i && (o[a] = [t[a]]);
  }
  return Object.keys(o).length > 0 ? o : void 0;
}
function J(e, t, n) {
  if (K(e, t)) return;
  if (e === void 0 && t !== void 0) {
    const a = n != null && n.cloneDiffValues ? O(t) : t;
    return [P(a)];
  }
  if (typeof e !== typeof t || e instanceof Date != t instanceof Date || e instanceof RegExp != t instanceof RegExp || Array.isArray(e) !== Array.isArray(t)) {
    const a = n != null && n.cloneDiffValues ? O(e) : e, c = n != null && n.cloneDiffValues ? O(t) : t;
    return [P(a), P(c)];
  }
  if (Array.isArray(e) && Array.isArray(t))
    return dt(e, t, n);
  if (E(e) && E(t))
    return ft(e, t, n);
  const u = n != null && n.cloneDiffValues ? O(e) : e, l = n != null && n.cloneDiffValues ? O(t) : t;
  return [P(u), P(l)];
}
function mt(e, t, n) {
  return J(e, t, n);
}
function vt(e, t, n) {
  const o = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
  for (const a in t) {
    if (a === "_t" || !a.startsWith("_")) continue;
    const c = parseInt(a.slice(1)), i = t[a];
    Array.isArray(i) && (i.length === 3 && i[2] === M ? o.add(c) : i.length === 3 && i[2] === F && r.set(c, i[1]));
  }
  const u = [];
  let l = 0;
  for (let a = 0; ; a++) {
    const c = a.toString(), i = t[c];
    if (i !== void 0)
      if (Array.isArray(i) && i.length === 1)
        u.push(i[0]);
      else {
        for (; l < e.length && o.has(l); )
          l++;
        l < e.length && (u.push(Y(e[l], i)), l++);
      }
    else {
      let f = !1;
      for (const [h, v] of r.entries())
        if (v === a) {
          u.push(e[h]), f = !0;
          break;
        }
      if (!f) {
        for (; l < e.length && (o.has(l) || r.has(l)); )
          l++;
        if (l < e.length)
          u.push(e[l]), l++;
        else
          break;
      }
    }
  }
  return u;
}
function yt(e, t, n) {
  const o = { ...e };
  for (const r in t) {
    const u = t[r];
    Array.isArray(u) ? u.length === 1 ? o[r] = u[0] : u.length === 3 && u[2] === M ? delete o[r] : u.length === 2 && (o[r] = u[1]) : o[r] = Y(o[r], u);
  }
  return o;
}
function Y(e, t, n) {
  if (t === void 0) return e;
  if (Array.isArray(t)) {
    if (t.length === 1)
      return t[0];
    if (t.length === 2)
      return t[1];
    if (t.length === 3 && t[2] === M)
      return;
  }
  return E(t) && t._t === "a" ? vt(e, t) : E(t) && E(e) ? yt(e, t) : e;
}
function le(e, t, n) {
  return Y(e, t);
}
function ht(e) {
  const t = { _t: "a" };
  for (const n in e)
    if (n !== "_t")
      if (n.startsWith("_")) {
        const o = parseInt(n.slice(1)), r = e[n];
        if (Array.isArray(r)) {
          if (r.length === 3 && r[2] === M)
            t[o] = [r[0]];
          else if (r.length === 3 && r[2] === F) {
            const u = r[1];
            t[`_${u}`] = [r[0], o, F];
          }
        }
      } else {
        const o = parseInt(n), r = e[n];
        if (Array.isArray(r) && r.length === 1)
          t[`_${o}`] = [r[0], M, M];
        else {
          const u = Q(r);
          u !== void 0 && (t[n] = u);
        }
      }
  return t;
}
function pt(e) {
  const t = {};
  for (const n in e) {
    const o = e[n];
    if (Array.isArray(o))
      o.length === 1 ? t[n] = [o[0], M, M] : o.length === 3 && o[2] === M ? t[n] = [o[0]] : o.length === 2 && (t[n] = [o[1], o[0]]);
    else {
      const r = Q(o);
      r !== void 0 && (t[n] = r);
    }
  }
  return t;
}
function Q(e) {
  if (e !== void 0) {
    if (Array.isArray(e)) {
      if (e.length === 1)
        return [e[0], M, M];
      if (e.length === 2)
        return [e[1], e[0]];
      if (e.length === 3 && e[2] === M)
        return [e[0]];
      if (e.length === 3 && e[2] === F)
        return e;
    }
    return E(e) && e._t === "a" ? ht(e) : E(e) ? pt(e) : e;
  }
}
function de(e) {
  return Q(e);
}
function gt(e, t, n) {
  const o = de(t);
  return le(e, o);
}
class _t {
  constructor(t) {
    ne(this, "options");
    this.options = t || {};
  }
  diff(t, n) {
    return mt(t, n, this.options);
  }
  patch(t, n) {
    return le(t, n, this.options);
  }
  reverse(t) {
    return de(t);
  }
  unpatch(t, n) {
    return gt(t, n, this.options);
  }
  clone(t) {
    return O(t);
  }
}
function St(e) {
  return new _t(e);
}
function bt({
  maxHistorySize: e = 10
}) {
  const t = St({
    objectHash: (y) => y._id || y.id,
    arrays: {
      detectMove: !0,
      includeValueOnMove: !1
    },
    cloneDiffValues: !1
  }), n = N(null), o = N([]), r = B(-1), u = N(null), l = S(() => r.value > 0), a = S(() => r.value < o.value.length - 1);
  function c(y) {
    if (n.value === null) {
      n.value = C(y), u.value = C(y), o.value = [{ delta: null }], r.value = 0;
      return;
    }
    const p = t.diff(u.value, y);
    if (r.value < o.value.length - 1 && (o.value = o.value.slice(0, r.value + 1)), o.value.push({ delta: p }), r.value++, u.value = C(y), o.value.length > e) {
      const b = o.value[1].delta;
      b && n.value && (n.value = t.patch(C(n.value), b)), o.value.shift(), r.value--;
    }
  }
  function i(y) {
    if (!n.value || y < 0 || y >= o.value.length)
      return;
    let p = C(n.value);
    for (let b = 1; b <= y; b++) {
      const w = o.value[b];
      w.delta && (p = t.patch(p, w.delta));
    }
    return p;
  }
  function f() {
    if (l.value) {
      r.value--;
      const y = i(r.value);
      return y && (u.value = C(y)), y;
    }
  }
  function h() {
    if (a.value) {
      r.value++;
      const y = i(r.value);
      return y && (u.value = C(y)), y;
    }
  }
  function v() {
    n.value = null, u.value = null, o.value = [], r.value = -1;
  }
  return {
    push: c,
    canUndo: l,
    canRedo: a,
    undo: f,
    redo: h,
    clear: v
  };
}
const wt = {
  async updateMenuModule(e, t) {
    const n = G.RequestRunner.operation(t, e.modules);
    if (n.validateError.length > 0)
      throw new Error(n.validateError.join(`
`));
    const o = {
      ...e,
      modules: n.result || e.modules,
      _rev: n.id ?? ""
    };
    return ie.cloneDeep({ menu: o, updateId: n.id ?? null });
  }
};
function fe() {
  return {
    _id: "",
    masterCatalogSetting: { inclusiveTaxes: [] },
    modules: {
      catalog: [],
      category: [],
      group: [],
      item: [],
      printDepartment: [],
      scheduler: [],
      takeaway: [],
      variant: [],
      sku: [],
      recipe: [],
      unit: [],
      cookingGuide: [],
      serveSequence: [],
      subCategory: []
    },
    updatedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
}
function Mt() {
  return {
    restaurants: [],
    menu: { ...fe(), _rev: "" },
    currentCatalogId: null,
    currentCategoryIds: null,
    currentSchedulerId: "",
    currentRestaurantAttrParams: {
      restaurantCustomAttributes: null,
      restaurantAttributes: null
    }
  };
}
const $t = Qe(
  "mf-menu-store-v2",
  () => {
    const e = Xe(Mt()), t = se(), n = tt(), o = S(() => e.menu.modules.serveSequence ? x(e.menu.modules.serveSequence, {
      selector: "position"
    }) : []), r = S(() => {
      var s;
      return ((s = t.currentBusiness.value) == null ? void 0 : s.menuVersion) || q.F_MENU_VERSION.v3;
    }), u = S(() => r.value === q.F_MENU_VERSION.v4), l = S(() => {
      const s = e.menu.modules.catalog;
      return x(s, { selector: "name" });
    }), a = S(() => {
      var d;
      let s = e.menu.modules.catalog.find(
        (m) => m._id === e.currentCatalogId
      );
      return s && ((d = s.linkItemRules) != null && d.length) && (s = T.populateCatalog({
        catalog: s,
        ruleGroupSettings: e.menu.ruleGroupSettings,
        items: e.menu.modules.item,
        ...e.currentRestaurantAttrParams ?? {}
      })), s;
    }), c = S(() => x(h.value.modules.scheduler, { selector: "name" })), i = S(() => e.menu.modules.scheduler.find(
      (s) => s._id === e.currentSchedulerId
    ));
    function f(s, d = !0) {
      e.menu = { ...s, _rev: e.menu._rev }, d && D.push(e.menu);
    }
    const h = N(fe()), v = B(!1), y = B(1);
    async function p() {
      var m;
      v.value = !0;
      let s = e.menu.modules.catalog;
      if (a.value && ((m = a.value.linkItemRules) != null && m.length)) {
        const g = T.populateCatalog({
          catalog: a.value,
          ruleGroupSettings: e.menu.ruleGroupSettings,
          items: e.menu.modules.item,
          ...e.currentRestaurantAttrParams ?? {}
        });
        s = e.menu.modules.catalog.map((_) => _._id === (g == null ? void 0 : g._id) ? g : _);
      }
      const d = await ce(
        {
          ...e.menu,
          modules: {
            ...e.menu.modules,
            catalog: s
          }
        },
        e.currentCatalogId,
        e.currentSchedulerId
      );
      if (e.currentSchedulerId != null) {
        const g = e.menu.modules.catalog.find(
          (R) => R._id === e.currentCatalogId
        ), _ = g ? g.override.scheduler[e.currentSchedulerId] : e.menu.modules.scheduler.find(
          (R) => R._id === e.currentSchedulerId
        ), A = new Set(((_ == null ? void 0 : _.items) || []).map((R) => R._id));
        d.modules.item = d.modules.item.filter((R) => A.has(R._id));
      }
      return h.value = d, v.value = !1, y.value = y.value + 1, d;
    }
    const w = ie.debounce(p, 200);
    let k = null, I = null;
    function X() {
      I && (I(), I = null), k && k.stop(), k = Ze(!0), I = k.run(() => et(
        () => [e.menu, e.currentCatalogId, e.currentSchedulerId],
        () => {
          v.value || w();
        }
      )) || null;
    }
    X();
    const me = S(() => [
      {
        label: "Main",
        value: null
      },
      ...l.value.map((s) => ({
        label: s.name,
        value: s._id
      }))
    ]), Z = S(() => {
      const s = h.value.modules.category;
      return [
        ...U.value.item.addon ? [
          {
            _id: e.currentCatalogId ? "" : null,
            name: "Addon",
            position: 0
          }
        ] : [],
        ...V(s, (d) => d.name)
      ];
    }), ve = S(() => {
      const s = h.value.modules.subCategory || [];
      return V(s, (d) => d.name);
    }), ee = S(() => {
      var d;
      let s = h.value.modules.item;
      if (a.value) {
        const m = (d = a.value) == null ? void 0 : d.override.items;
        s = s.filter((g) => {
          var A;
          const _ = (A = m == null ? void 0 : m[g._id]) == null ? void 0 : A.find(
            (R) => !R.combinationKey
          );
          return (_ == null ? void 0 : _.orderFrom) !== ue.F_ORDER_FROM.enum.NONE;
        });
      }
      return V(s, (m) => m.code || m.name);
    }), ye = S(() => {
      const s = e.menu.modules.category, d = V(s, (g) => g.name).reduce(
        (g, _, A) => (g[_._id] = A, g),
        { "": 0 }
      ), m = e.menu.modules.item;
      return V(
        m,
        (g) => g.code || g.name
      ).reduce((g, _, A) => (g[_._id] = A + d[_.category || ""], g), {});
    }), he = S(() => oe(j.value, Z.value, ee.value)), pe = S(() => {
      const s = U.value.item.addon ? [
        {
          _id: e.currentCatalogId ? "" : null,
          name: "Addon",
          position: 0
        },
        ...e.menu.modules.category
      ] : e.menu.modules.category;
      return oe(
        e.menu.modules.variant,
        s,
        e.menu.modules.item
      );
    }), j = S(() => {
      const s = h.value.modules.variant;
      return x(s, { selector: "name" });
    });
    function ge(s = []) {
      const d = s.map((g) => j.value.find((_) => _._id === g)).filter((g) => !!g);
      return T.fullVariantCombination(d).reduce((g, _) => (g[_.combinationKey] = _.selectionName.join(" / "), g), {});
    }
    const _e = S(() => V(h.value.modules.group, (s) => s.name)), Se = S(() => x(h.value.modules.takeaway, { selector: "name" })), be = S(() => x(e.menu.modules.printDepartment || [], {
      selector: "name"
    })), we = S(() => x(e.menu.modules.unit, { selector: "name" })), Me = S(() => x(Le(e.menu.modules.recipe, e.menu.modules.sku), {
      selector: "name"
    })), Ae = S(() => e.menu.modules.cookingGuide), Ce = S(() => {
      const s = ze(e.menu.modules.sku);
      return x(s, { selector: "code" });
    }), ke = S(() => {
      const s = /* @__PURE__ */ new Map();
      return e.menu.modules.item.forEach((d) => {
        var m, g;
        d.code && s.set(d.code, (s.get(d.code) || 0) + 1), (g = (m = d.variant) == null ? void 0 : m.variantCombinations) == null || g.forEach((_) => {
          _.code && s.set(_.code, (s.get(_.code) || 0) + 1);
        });
      }), s;
    }), U = S(() => {
      const s = !e.currentCatalogId, d = r.value === q.F_MENU_VERSION.v4, m = !e.currentSchedulerId, g = {
        create: s && m,
        edit: s && m,
        delete: s && m
      };
      return {
        item: {
          ...g,
          edit: d || m,
          sort: d && s && m,
          addon: d,
          code: m,
          name: s && m,
          description: s && m,
          category: m,
          thumbnail: s && m,
          orderFrom: (d || s) && m,
          orderFromNone: s,
          orderFromActiveOnly: !d,
          openPrice: s && m,
          unit: s && m,
          price: s,
          editPrice: s,
          minPrice: d && m,
          costing: d && s && m,
          tax: s && m,
          variant: s && m,
          maxVariant: d ? 4 : 2,
          group: s && m,
          overrideProductGroup: d && m,
          inventory: (d || s) && m,
          bindToVariant: d,
          print: s && m,
          toRoot: d && m,
          takeaway: (_) => !!_ && (d || s) && m,
          multiScheduler: d,
          canRearrange: d,
          printConfig: d,
          takeawayConfig: d
        },
        category: {
          ...g,
          sort: s,
          overrideSequence: d
        },
        group: {
          ...g,
          edit: d || m,
          sort: s,
          applyProduct: s,
          showAppliedProduct: m,
          type: !d,
          insertRawItems: !d,
          multiScheduler: d,
          linkAddonName: d
        },
        scheduler: {
          override: d
        },
        takeaway: {
          ...g,
          edit: (d || s) && m,
          name: s
        },
        catalog: {
          name: !s,
          defaultOrderFrom: d,
          markup: d
        }
      };
    });
    function W(s) {
      return `${s.systemCode}/${s.taxCode}`;
    }
    function Ie(s) {
      const [d, m] = s.split("/");
      return { systemCode: d, taxCode: m };
    }
    function Ee(s, d, m) {
      return (s ?? d ?? []).map((g) => W(g)).find((g) => m.some((_) => _.value === g));
    }
    const Re = S(() => {
      var g;
      const s = ["SV5", "SV10"], d = Ge.Country.getCountryFlattenTaxes(
        se().currentCountry.value
      ), m = d.filter((_) => u.value || !s.includes(_.taxCode)).map((_) => ({
        label: `${_.name} (${Be(_.rate)}%)`,
        value: W(_),
        meta: {
          rate: _.rate
        }
      }));
      return m.sort((_, A) => _.label.localeCompare(A.label, void 0, { numeric: !0 })), {
        systemCode: (g = d[0]) == null ? void 0 : g.systemCode,
        countryTaxOptions: m
      };
    }), xe = async () => {
      const s = await Ye.readMenu();
      e.menu = s ?? e.menu, te(null);
    }, Oe = async () => {
      const { fetchAllRestaurants: s } = Je();
      await s({
        onBatchEnd: (d) => {
          e.restaurants = d;
        }
      });
    }, te = (s) => {
      e.currentCatalogId = s;
    }, De = (s) => {
      e.currentSchedulerId = s;
    }, Ve = (s) => {
      e.currentCategoryIds = s;
    }, Te = (s) => {
      e.currentRestaurantAttrParams = s;
    }, Fe = async (s, d) => {
      const m = Array.isArray(s) ? s : [s];
      if (!m.length) return;
      const g = m.map((_) => ({
        ..._,
        _id: e.menu._id,
        _rev: e.menu._rev
      }));
      try {
        if (d || g.some(
          (A) => [G.F_OPERATION_TYPE.delete, G.F_OPERATION_TYPE.deleteMany].includes(A.operation)
        )) {
          const A = await He(g, e.menu.modules);
          if (A !== !0) throw new Error(A);
        }
        const _ = await wt.updateMenuModule(e.menu, g);
        return f(_.menu), _;
      } catch (_) {
        n.open({
          type: "error",
          title: "Failed to update menu",
          message: _
        });
      }
    }, Ke = async (s) => {
      const d = {
        ...e.menu,
        masterCatalogSetting: s.masterCatalogSetting,
        lowStockThreshold: s.lowStockThreshold,
        priceTierSettings: s.priceTierSettings
      };
      return f(d), d;
    }, D = bt({ maxHistorySize: 50 }), Pe = S(() => D.canUndo.value), $e = S(() => D.canRedo.value), Ne = () => {
      const s = D.undo();
      s && f(s, !1);
    }, je = () => {
      const s = D.redo();
      s && f(s, !1);
    }, Ue = () => {
      D.clear();
    };
    return {
      state: e,
      // loading state
      workerIsComputing: v,
      mergedMenuChangeId: y,
      // async computed state
      mergedMenu: h,
      // Computed
      version: r,
      isV4: u,
      serveSequence: o,
      catalogs: l,
      currentCatalog: a,
      schedulers: c,
      currentScheduler: i,
      catalogOptions: me,
      categories: Z,
      subcategories: ve,
      items: ee,
      sortedItems: ye,
      itemCodeMap: ke,
      categorizedItems: he,
      categorizedRawMenuItems: pe,
      variants: j,
      groups: _e,
      takeaways: Se,
      printDepartments: be,
      units: we,
      recipes: Me,
      cookingGuide: Ae,
      skus: Ce,
      rules: U,
      taxSetting: Re,
      // Action
      setMenu: f,
      readMenu: xe,
      readRestaurants: Oe,
      selectCatalog: te,
      selectScheduler: De,
      selectCategory: Ve,
      setRestaurantAttrParams: Te,
      updateMenu: Fe,
      updateMenuSetting: Ke,
      computeMergedMenu: p,
      // helper
      encodeTax: W,
      decodeTax: Ie,
      getSelectedTax: Ee,
      mapVariant: ge,
      // history
      canUndoHistory: Pe,
      canRedoHistory: $e,
      undoHistory: Ne,
      redoHistory: je,
      // cleanup
      $dispose: () => {
        w.cancel(), I && (I(), I = null), k && (k.stop(), k = null), Ue();
      },
      $reinitialize: () => {
        X();
      }
    };
  }
);
export {
  Tt as a,
  Ft as b,
  Kt as c,
  Pt as f,
  Vt as g,
  Dt as m,
  it as o,
  $t as u
};
