import { defineComponent as ne, computed as g, reactive as oe, resolveComponent as B, openBlock as a, createElementBlock as y, createVNode as m, withCtx as k, createElementVNode as n, toDisplayString as p, unref as i, createBlock as I, createCommentVNode as v, Fragment as E, renderList as C, nextTick as te } from "vue";
import { a as le, t as ie, v as se, F as de } from "./app-Bpb4394e.js";
import { a as h, c as re } from "./number-BVg7EKPV.js";
import { u as ae } from "./use-inventory-binding-dialog-zJqVnpvd.js";
import { b as D, c as G } from "./rules-CoimOM7X.js";
import { _ as ue, c as me, d as ce } from "./FmUnitInput.vue_vue_type_script_setup_true_lang-Mic67Qjt.js";
import { useI18n as pe } from "@feedmepos/mf-common";
const ye = { class: "flex flex-col gap-24" }, fe = { class: "flex flex-col gap-16" }, ve = { class: "flex items-center" }, be = { class: "flex-1 fm-typo-en-title-sm-600" }, _e = { class: "flex flex-col" }, ge = { class: "flex py-8 px-12 border-b border-fm-color-neutral-gray-200 bg-fm-color-neutral-gray-100 h-[44px] items-center" }, xe = { class: "fm-typo-en-body-md-600 text-fm-color-typo-secondary" }, he = { class: "flex flex-col w-full border-b border-fm-color-neutral-gray-100" }, Ve = {
  class: "flex items-center",
  style: { "min-height": "48px" }
}, Be = { class: "flex-1 fm-typo-en-body-md-400 py-8 px-12" }, ke = { class: "flex flex-col" }, Ie = { class: "line-clamp-1" }, we = { class: "w-[200px]" }, Fe = {
  key: 0,
  class: "w-[52px]"
}, Ue = {
  key: 0,
  class: "flex gap-4 text-fm-color-system-error-400 fm-typo-en-body-sm-400 px-8"
}, Ee = {
  key: 0,
  class: "flex flex-col gap-16"
}, Ce = { class: "flex items-center" }, Ge = { class: "flex-1 fm-typo-en-title-sm-600" }, Ae = { class: "flex flex-col" }, Re = { class: "flex py-8 px-12 border-b border-fm-color-neutral-gray-200 bg-fm-color-neutral-gray-100 h-[44px] items-center" }, Se = { class: "fm-typo-en-body-md-600 text-fm-color-typo-secondary" }, Pe = { class: "flex flex-col w-full border-b border-fm-color-neutral-gray-100" }, Te = {
  key: 0,
  class: "flex gap-4 text-fm-color-system-error-400 fm-typo-en-body-sm-400"
}, Ne = {
  class: "flex items-center",
  style: { "min-height": "48px" }
}, Me = { class: "flex-1 fm-typo-en-body-md-400 py-8 px-12" }, De = { class: "flex flex-col" }, Ke = { class: "w-[200px]" }, $e = {
  key: 0,
  class: "w-[52px]"
}, ze = { class: "flex flex-col gap-16" }, je = { class: "flex items-center" }, Le = { class: "flex-1 fm-typo-en-title-sm-600" }, Oe = { class: "flex flex-col" }, Ye = { class: "flex py-8 px-12 border-b border-fm-color-neutral-gray-200 bg-fm-color-neutral-gray-100 h-[44px] items-center" }, We = { class: "flex-1 fm-typo-en-body-md-600 text-fm-color-typo-secondary" }, qe = { class: "fm-typo-en-body-md-600 text-fm-color-typo-secondary w-[150px] px-16" }, He = {
  class: "flex items-center w-full",
  style: { "min-height": "48px" }
}, Je = { class: "flex-1 fm-typo-en-body-md-400 py-8 px-12" }, Qe = { class: "line-clamp-1 w-full" }, Xe = { class: "w-[150px]" }, Ze = {
  key: 0,
  class: "w-[52px]"
}, en = { class: "flex flex-col px-12 pb-4" }, an = /* @__PURE__ */ ne({
  __name: "InventoryBindingForm",
  props: {
    id: {},
    modelValue: { default: () => [] },
    excludeBindingId: {},
    rules: { default: () => [] },
    iframeMode: { type: Boolean },
    disabled: { type: Boolean },
    readonly: { type: Boolean },
    enableIngredientGroup: { type: Boolean }
  },
  emits: ["update:modelValue", "click:pick-inventory-bindings"],
  setup(K, { emit: $ }) {
    const { t: u } = pe(), x = le(), A = g(() => x.recipeById), w = g(() => x.skuById), z = g(() => x.skuGroupById), s = K, b = $, R = g(() => ie({
      parentId: s.id,
      bindings: s.modelValue,
      recipes: x.recipes.map((o) => o._id === s.id ? {
        ...o,
        contains: s.modelValue
      } : o),
      skus: x.skus.map((o) => o._id === s.id ? {
        ...o,
        convert: {
          inventoryBindings: s.modelValue
        }
      } : o),
      skuGroups: x.skuGroups
    }).inventoryBindings.filter((o) => "error" in o && o.error).map((o) => o).filter((o) => o.reason === se.enum.circularDependency)), F = g(
      () => new Set(R.value.map((e) => {
        var l;
        return (l = e.bindingPaths[0]) == null ? void 0 : l.id;
      }))
    ), j = g(() => s.modelValue.filter((e) => e.type === "RECIPE")), { promptIngredientsBinding: L, promptRecipesBinding: O, promptIngredientGroupsBinding: Y } = ae();
    async function W() {
      if (s.iframeMode) {
        b("click:pick-inventory-bindings", "RECIPE");
        return;
      }
      O(
        s.modelValue,
        (e) => b("update:modelValue", e),
        s.id ? [s.id] : []
      );
    }
    const q = g(() => s.modelValue.filter((e) => e.type === "SKU_GROUP"));
    async function H() {
      Y(
        s.modelValue,
        (e) => b("update:modelValue", e),
        s.id ? [s.id] : []
      );
    }
    const S = g(() => s.modelValue.filter((e) => e.type === "SKU") ?? []);
    async function J() {
      if (s.iframeMode) {
        b("click:pick-inventory-bindings", "SKU");
        return;
      }
      L(
        s.modelValue,
        (e) => b("update:modelValue", e),
        s.excludeBindingId,
        s.id ? [s.id] : []
      );
    }
    function P(e, l, o) {
      const r = +l;
      let f = o;
      l.length > 0 && isFinite(r) ? f = o.map((c) => (c.id !== e || (c.amount = re(r)), c)) : f = o.map((c) => (c.id !== e || (c.amount = { amount: 0, precision: 0 }), c)), b("update:modelValue", f);
    }
    function U(e, l) {
      const o = l.filter((r) => r.id !== e);
      b("update:modelValue", o);
    }
    function T(e) {
      var o, r;
      if (e.type !== de.enum.SKU)
        throw new Error("Non-SKU binding does not have unit");
      if (!((o = w.value[e.id]) == null ? void 0 : o.unit))
        throw new Error(`Cannot find ingredient: ${e.id}`);
      return (r = w.value[e.id]) == null ? void 0 : r.unit;
    }
    function Q(e) {
      return { amount: e.amount, measurement: e.measurement };
    }
    function X(e, l, o) {
      const r = o.map((f) => f.id !== e ? f : {
        ...f,
        amount: l.amount,
        measurement: l.measurement
      });
      b("update:modelValue", r);
    }
    const N = oe({});
    function Z(e) {
      return N[e.id] ?? {
        modelValue: {
          amount: e.amount,
          measurement: e.measurement
        },
        amountAsString: h(e.amount)
      };
    }
    function ee(e, l) {
      te(() => {
        N[e.id] = {
          modelValue: {
            amount: e.amount,
            measurement: e.measurement
          },
          amountAsString: l
        };
      });
    }
    return (e, l) => {
      const o = B("FmFormGroup"), r = B("FmButton"), f = B("FmTooltip"), c = B("FmTextField"), M = B("FmIcon");
      return a(), y("div", ye, [
        m(o, {
          disabled: e.disabled,
          "model-value": e.modelValue,
          rules: [
            ...e.rules,
            () => R.value.length ? "There is a binding error in your configuration" : !0
          ]
        }, {
          label: k(() => l[0] || (l[0] = [
            n("div", { class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary" }, " Define the inventory bindings for this recipe or ingredient. ", -1)
          ])),
          _: 1
        }, 8, ["disabled", "model-value", "rules"]),
        n("div", fe, [
          n("div", ve, [
            n("div", be, p(i(u)("inventory.inventoryBindings.recipe")), 1),
            e.readonly ? v("", !0) : (a(), I(r, {
              key: 0,
              variant: "secondary",
              "text-color": "primary",
              "border-color": "primary",
              label: i(u)("inventory.inventoryBindings.linkRecipe"),
              "prepend-icon": "link",
              onClick: W
            }, null, 8, ["label"]))
          ]),
          n("div", _e, [
            n("div", ge, [
              n("div", xe, p(i(u)("inventory.inventoryBindings.recipe")), 1),
              l[1] || (l[1] = n("div", { class: "w-[200px]" }, null, -1)),
              l[2] || (l[2] = n("div", { class: "w-[52px]" }, null, -1))
            ]),
            (a(!0), y(E, null, C(j.value, (t) => (a(), I(o, {
              key: t.id,
              disabled: e.disabled,
              modelValue: +i(h)(t.amount ?? { amount: 1, precision: 0 }),
              rules: [i(D)(0), i(G)(-1)]
            }, {
              default: k(({ invalid: V }) => {
                var _;
                return [
                  n("div", he, [
                    n("div", Ve, [
                      n("div", Be, [
                        n("div", ke, [
                          m(f, {
                            zIndex: 51,
                            variant: "plain",
                            placement: "top",
                            content: (_ = A.value[t.id]) == null ? void 0 : _.name
                          }, {
                            default: k(() => {
                              var d;
                              return [
                                n("span", Ie, p((d = A.value[t.id]) == null ? void 0 : d.name), 1)
                              ];
                            }),
                            _: 2
                          }, 1032, ["content"])
                        ])
                      ]),
                      n("div", we, [
                        m(c, {
                          class: "w-full",
                          disabled: e.disabled,
                          modelValue: +i(h)(t.amount ?? { amount: 1, precision: 0 }),
                          "onUpdate:modelValue": (d) => P(t.id, d, e.modelValue),
                          invalid: V
                        }, null, 8, ["disabled", "modelValue", "onUpdate:modelValue", "invalid"])
                      ]),
                      e.readonly ? v("", !0) : (a(), y("div", Fe, [
                        m(r, {
                          disabled: e.disabled,
                          type: "button",
                          variant: "plain",
                          icon: "delete",
                          "icon-color": "#000000",
                          onClick: (d) => U(t.id, e.modelValue)
                        }, null, 8, ["disabled", "onClick"])
                      ]))
                    ]),
                    F.value.has(t.id) ? (a(), y("div", Ue, [
                      m(M, {
                        name: "error",
                        color: "system-error-300",
                        size: "sm"
                      }),
                      n("div", null, p(i(u)("inventory.inventoryBindings.circularError")), 1)
                    ])) : v("", !0)
                  ])
                ];
              }),
              _: 2
            }, 1032, ["disabled", "modelValue", "rules"]))), 128))
          ])
        ]),
        e.enableIngredientGroup ? (a(), y("div", Ee, [
          n("div", Ce, [
            n("div", Ge, p(i(u)("inventory.inventoryBindings.ingredientGroup")), 1),
            e.readonly ? v("", !0) : (a(), I(r, {
              key: 0,
              variant: "secondary",
              "text-color": "primary",
              "border-color": "primary",
              label: i(u)("inventory.inventoryBindings.linkIngredientGroup"),
              "prepend-icon": "link",
              onClick: H
            }, null, 8, ["label"]))
          ]),
          n("div", Ae, [
            n("div", Re, [
              n("div", Se, p(i(u)("inventory.inventoryBindings.ingredientGroup")), 1),
              l[3] || (l[3] = n("div", { class: "w-[200px]" }, null, -1)),
              l[4] || (l[4] = n("div", { class: "w-[52px]" }, null, -1))
            ]),
            (a(!0), y(E, null, C(q.value, (t) => (a(), I(o, {
              key: t.id,
              disabled: e.disabled,
              modelValue: +i(h)(t.amount ?? { amount: 1, precision: 0 }),
              rules: [i(D)(0), i(G)(0)]
            }, {
              default: k(({ invalid: V }) => {
                var _;
                return [
                  n("div", Pe, [
                    F.value.has(t.id) ? (a(), y("div", Te, [
                      m(M, {
                        name: "error",
                        color: "system-error-300",
                        size: "sm"
                      }),
                      n("div", null, p(i(u)("inventory.inventoryBindings.circularError")), 1)
                    ])) : v("", !0),
                    n("div", Ne, [
                      n("div", Me, [
                        n("div", De, p((_ = z.value[t.id]) == null ? void 0 : _.name), 1)
                      ]),
                      n("div", Ke, [
                        m(c, {
                          class: "w-full",
                          disabled: e.disabled,
                          modelValue: +i(h)(t.amount ?? { amount: 1, precision: 0 }),
                          "onUpdate:modelValue": (d) => P(t.id, d, e.modelValue),
                          invalid: V
                        }, null, 8, ["disabled", "modelValue", "onUpdate:modelValue", "invalid"])
                      ]),
                      e.readonly ? v("", !0) : (a(), y("div", $e, [
                        m(r, {
                          disabled: e.disabled,
                          type: "button",
                          variant: "plain",
                          icon: "delete",
                          "icon-color": "#000000",
                          onClick: (d) => U(t.id, e.modelValue)
                        }, null, 8, ["disabled", "onClick"])
                      ]))
                    ])
                  ])
                ];
              }),
              _: 2
            }, 1032, ["disabled", "modelValue", "rules"]))), 128))
          ])
        ])) : v("", !0),
        n("div", ze, [
          n("div", je, [
            n("div", Le, p(i(u)("inventory.inventoryBindings.ingredient")), 1),
            e.readonly ? v("", !0) : (a(), I(r, {
              key: 0,
              variant: "secondary",
              "text-color": "primary",
              "border-color": "primary",
              label: i(u)("inventory.inventoryBindings.linkIngredient"),
              "prepend-icon": "link",
              onClick: J
            }, null, 8, ["label"]))
          ]),
          n("div", Oe, [
            n("div", Ye, [
              n("div", We, p(i(u)("inventory.inventoryBindings.ingredient")), 1),
              n("div", qe, p(i(u)("inventory.inventoryBindings.amount")), 1),
              l[5] || (l[5] = n("div", { class: "w-[52px]" }, null, -1))
            ]),
            (a(!0), y(E, null, C(S.value, (t, V) => {
              var _;
              return a(), y("div", {
                key: t.id,
                class: "flex flex-col border-b border-fm-color-neutral-gray-100"
              }, [
                n("div", He, [
                  n("div", Je, [
                    m(f, {
                      zIndex: 51,
                      variant: "plain",
                      placement: "top",
                      content: (_ = w.value[t.id]) == null ? void 0 : _.name
                    }, {
                      default: k(() => {
                        var d;
                        return [
                          n("span", Qe, p((d = w.value[t.id]) == null ? void 0 : d.name), 1)
                        ];
                      }),
                      _: 2
                    }, 1032, ["content"])
                  ]),
                  n("div", Xe, [
                    m(ue, {
                      "model-value": Q(t),
                      "onUpdate:modelValue": (d) => d && X(t.id, d, e.modelValue),
                      "onUpdate:amount": (d) => ee(t, d),
                      unit: T(t),
                      placement: V === S.value.length - 1 ? "top" : "bottom",
                      "convert-amount-on-change-unit": "",
                      disabled: e.readonly
                    }, null, 8, ["model-value", "onUpdate:modelValue", "onUpdate:amount", "unit", "placement", "disabled"])
                  ]),
                  e.readonly ? v("", !0) : (a(), y("div", Ze, [
                    m(r, {
                      disabled: e.disabled,
                      type: "button",
                      variant: "tertiary",
                      icon: "delete",
                      onClick: (d) => U(t.id, e.modelValue)
                    }, null, 8, ["disabled", "onClick"])
                  ]))
                ]),
                n("div", en, [
                  m(o, {
                    modelValue: +i(h)(t.amount ?? { amount: 1, precision: 0 }),
                    rules: [i(G)(0)]
                  }, null, 8, ["modelValue", "rules"]),
                  m(o, {
                    modelValue: Z(t),
                    rules: [
                      i(me)(),
                      i(ce)(T(t)),
                      () => F.value.has(t.id) ? i(u)("inventory.inventoryBindings.invalidWarning") : !0
                    ]
                  }, null, 8, ["modelValue", "rules"])
                ])
              ]);
            }), 128))
          ])
        ])
      ]);
    };
  }
});
export {
  an as _
};
