import { u as h, A as n, M as d, am as _ } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import "@feedmepos/ui-library";
class u {
  constructor(t, e, r, a) {
    this.catalog = t, this.menuStore = e, this.updateMenu = r, this.dialog = a;
  }
  static getItemAvailable(t) {
    return Object.entries(t.override.items).filter(([, e]) => {
      const r = e.find(
        (o) => !o.combinationKey
      ), a = h().version === n.F_MENU_VERSION.enum.v4;
      return (r == null ? void 0 : r.orderFrom) !== n.F_ORDER_FROM.enum.NONE && (a || (r == null ? void 0 : r.category) !== "");
    }).map(([e]) => e);
  }
  static getSellingItem(t, e) {
    return u.getItemAvailable(t).filter(
      (r) => {
        var a;
        return !!((a = e.get(r)) != null && a.category);
      }
    );
  }
  // createCatalog() {
  //   this.promptCatalogDialog(undefined, this.create.bind(this));
  // }
  //
  // duplicateCatalog() {
  //   if (!this.catalog) return;
  //   this.promptCatalogDialog(
  //     {
  //       ...this.catalog,
  //       name: `${this.catalog.name} clone`,
  //     },
  //     this.create.bind(this),
  //   );
  // }
  getCatalog() {
    return this.catalog;
  }
  async update() {
    this.catalog && await this.updateMenu(
      {
        module: n.F_MENU_MODULE_V4_KEY.enum.catalog,
        operation: d.F_OPERATION_TYPE.update,
        data: this.catalog
      },
      this.catalog.name
    );
  }
  getCatalogGroup(t) {
    var e;
    return this.catalog ? ((e = this.catalog) == null ? void 0 : e.override.groups[t]) ?? { _id: t, groupKey: "" } : null;
  }
  getCatalogSchedulerGroup(t, e) {
    var r, a, o;
    return this.catalog ? ((o = (a = (r = this.catalog) == null ? void 0 : r.override.scheduler[e]) == null ? void 0 : a.groups) == null ? void 0 : o.find((i) => i._id === t)) ?? {
      _id: t,
      groupKey: ""
    } : null;
  }
  overrideCatalogGroups(t) {
    if (!this.catalog) return null;
    const e = {
      ...this.catalog.override.groups,
      ...t.reduce((r, a) => (r[a._id] = {
        _id: a._id,
        groupKey: "",
        items: a.items
      }, r), {})
    };
    this.catalog = {
      ...this.catalog,
      override: { ...this.catalog.override, groups: e }
    };
  }
  overrideCatalogSchedulerGroups(t, e) {
    var i;
    if (!this.catalog) return null;
    const r = ((i = this.catalog.override.scheduler[e]) == null ? void 0 : i.groups) ?? [], a = t.map((l) => l._id), o = [
      ...r.filter((l) => !a.includes(l._id)),
      ...t.map((l) => ({
        _id: l._id,
        groupKey: "",
        items: l.items
      }))
    ];
    this.catalog = {
      ...this.catalog,
      override: {
        ...this.catalog.override,
        scheduler: {
          ...this.catalog.override.scheduler,
          [e]: {
            ...this.catalog.override.scheduler[e],
            _id: e,
            groups: o
          }
        }
      }
    };
  }
  getCatalogScheduler(t) {
    return this.catalog ? this.catalog.override.scheduler[t] ?? { _id: t } : null;
  }
  overrideCatalogScheduler(t) {
    if (!this.catalog) return null;
    this.catalog = {
      ...this.catalog,
      override: {
        ...this.catalog.override,
        scheduler: {
          ...this.catalog.override.scheduler,
          [t._id]: t
        }
      }
    };
  }
  overrideCatalogSchedulerItems(t, e) {
    if (!this.catalog) return null;
    const r = this.getCatalogScheduler(e);
    if (!r) return;
    const a = t.reduce((i, l) => {
      var c;
      const m = ((c = r.items) == null ? void 0 : c.filter((s) => s._id == l)) ?? [];
      return i = i.concat(
        _(l, m, (s) => ({
          ...s,
          orderFrom: null
        }))
      ), i;
    }, []), o = {
      ...r,
      items: a
    };
    this.overrideCatalogScheduler(o);
  }
  injectCatalogToMenu() {
    const t = this.catalog;
    if (!t) return null;
    const e = this.menuStore.state.menu, r = e.modules.catalog.map((a) => a._id === t._id ? t : a);
    return {
      ...e,
      modules: {
        ...e.modules,
        catalog: r
      }
    };
  }
  async unlinkProduct(t) {
    this.catalog && (this.updateItemAvailable(
      u.getItemAvailable(this.catalog).filter((e) => !t.includes(e))
    ), await this.update());
  }
  async create(t) {
    await this.updateMenu(
      {
        module: n.F_MENU_MODULE_V4_KEY.enum.catalog,
        operation: d.F_OPERATION_TYPE.create,
        data: t
      },
      t.name
    );
    const e = this.menuStore.state.menu.modules.catalog, r = e[e.length - 1]._id;
    this.menuStore.selectCatalog(r);
  }
  updateItemAvailable(t) {
    const e = this.catalog;
    if (!e) return null;
    const a = this.menuStore.state.menu.modules.item.map((o) => o._id).reduce((o, i) => {
      const l = e.override.items[i];
      return o[i] = p(
        i,
        l,
        t.includes(i),
        this.menuStore.version === n.F_MENU_VERSION.enum.v3
      ), o;
    }, {});
    this.catalog = {
      ...e,
      override: { ...e.override, items: a }
    };
  }
}
function p(g, t, e, r) {
  const a = t == null ? void 0 : t.find((l) => !l.combinationKey), o = e ? (a == null ? void 0 : a.orderFrom) !== n.F_ORDER_FROM.enum.NONE ? a == null ? void 0 : a.orderFrom : null : n.F_ORDER_FROM.enum.NONE, i = r ? e ? null : "" : a == null ? void 0 : a.category;
  return [
    {
      ...a ?? {},
      orderFrom: o,
      category: i,
      _id: g
    },
    ...(t == null ? void 0 : t.filter((l) => !!l.combinationKey)) ?? []
  ];
}
export {
  u as C,
  p as h
};
