import { a as g } from "./FmMultiselectDialog-D-38fV59.js";
import { a as P } from "./app-Bpb4394e.js";
import { useDialog as E } from "@feedmepos/ui-library";
function A() {
  const u = P(), _ = E();
  async function b(e, m, s, f = []) {
    var i, o;
    const y = e.filter((n) => n.type === "SKU"), l = e.filter((n) => n.type !== "SKU"), c = /* @__PURE__ */ new Set();
    if (s)
      for (const n of u.skus)
        !f.some((a) => a === n._id) && n._id !== s && !((o = (i = n.convert) == null ? void 0 : i.inventoryBindings) != null && o.some((a) => a.id === s)) || c.add(n._id);
    const t = u.skus.map((n) => ({
      label: n.name,
      sublabel: `${n.code}${c.has(n._id) ? " (Cannot bind, will cause recursive binding)" : ""}`,
      value: n,
      disabled: c.has(n._id)
    })), p = t.filter(
      (n) => y.find((d) => d.id === n.value._id) && !c.has(n.value._id)
    ).map((n) => n.value);
    _.open({
      title: "Select ingredients",
      contentComponent: g,
      contentComponentProps: {
        items: t,
        modelValue: p,
        virtualScroll: !0
      },
      primaryActions: {
        text: "Confirm",
        close: !0
      },
      secondaryActions: {
        text: "Close",
        close: !0,
        variant: "tertiary"
      }
    }).onPrimary((n) => {
      const d = [
        ...l,
        ...e.filter((a) => n.find((r) => r._id === a.id)),
        ...n.filter((a) => !e.find((r) => r.id === a._id)).map((a) => ({
          type: "SKU",
          id: a._id,
          amount: {
            amount: 1,
            precision: 0
          }
        }))
      ];
      m(d);
    });
  }
  async function v(e, m, s = []) {
    var p;
    const f = e.filter((i) => i.type === "RECIPE"), y = e.filter((i) => i.type !== "RECIPE"), l = /* @__PURE__ */ new Set();
    if (s.length)
      for (const i of u.recipes) {
        const o = ((p = u.recipeBindingFlattenCache[i._id]) == null ? void 0 : p.inventoryBindings) ?? [], n = o.some(
          (r) => r.bindingPaths.some((S) => s.some((h) => h === S.id))
        ), d = o.some(
          (r) => "success" in r && s.includes(r.ingredient._id)
        );
        (s.some((r) => r === i._id) || n || d) && l.add(i._id);
      }
    const c = u.recipes.filter((i) => !s.includes(i._id)).map((i) => ({
      label: i.name,
      sublabel: l.has(i._id) ? "(Cannot bind, will cause recursive binding)" : `${i.contains.length} inventory bindings`,
      value: i,
      disabled: l.has(i._id)
    })), t = c.filter(
      (i) => f.find((o) => o.id === i.value._id) && !l.has(i.value._id)
    ).map((i) => i.value);
    _.open({
      title: "Select recipes",
      contentComponent: g,
      contentComponentProps: {
        items: c,
        modelValue: t,
        virtualScroll: !0
      },
      primaryActions: {
        text: "Confirm",
        close: !0
      },
      secondaryActions: {
        text: "Close",
        close: !0,
        variant: "tertiary"
      }
    }).onPrimary((i) => {
      const o = [
        ...e.filter((n) => i.find((d) => d._id === n.id)),
        ...i.filter((n) => !e.find((d) => d.id === n._id)).map((n) => ({
          type: "RECIPE",
          id: n._id,
          amount: {
            amount: 1,
            precision: 0
          }
        })),
        ...y
      ];
      m(o);
    });
  }
  async function C(e, m, s = []) {
    const f = e.filter((t) => t.type === "SKU_GROUP"), y = e.filter((t) => t.type !== "SKU_GROUP"), l = u.skuGroups.filter((t) => !s.includes(t._id)).map((t) => ({
      label: t.name,
      value: t
    })), c = l.filter((t) => f.find((p) => p.id === t.value._id)).map((t) => t.value);
    _.open({
      title: "Select ingredient group",
      contentComponent: g,
      contentComponentProps: {
        items: l,
        modelValue: c,
        virtualScroll: !0
      },
      primaryActions: {
        text: "Confirm",
        close: !0
      },
      secondaryActions: {
        text: "Close",
        close: !0,
        variant: "tertiary"
      }
    }).onPrimary((t) => {
      const p = [
        ...e.filter((i) => t.find((o) => o._id === i.id)),
        ...t.filter((i) => !e.find((o) => o.id === i._id)).map((i) => ({
          type: "SKU_GROUP",
          id: i._id,
          amount: {
            amount: 1,
            precision: 0
          }
        })),
        ...y
      ];
      m(p);
    });
  }
  return {
    promptIngredientsBinding: b,
    promptRecipesBinding: v,
    promptIngredientGroupsBinding: C
  };
}
export {
  A as u
};
