import { defineComponent as X, ref as C, computed as $, watch as j, resolveComponent as g, createBlock as L, openBlock as d, unref as t, withCtx as y, createElementVNode as r, createVNode as u, createElementBlock as h, Fragment as q, renderList as Y, createCommentVNode as B, toDisplayString as A, withModifiers as oe } from "vue";
import { u as Z, J as K, a5 as ae, G as ne, B as re, I as le } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { useI18n as ee, useCoreStore as ie } from "@feedmepos/mf-common";
import { components as te, FmButtonVariant as N, useDialog as ce, useSnackbar as ue, useBreakpoints as me, FmButtonColorThemeVariant as W } from "@feedmepos/ui-library";
import { u as de, m as E, _ as Q, b as pe, c as _e, f as P } from "./RuleView.vue_vue_type_script_setup_true_lang-DRUQOy4h.js";
import { _ as se } from "./app-D-J2cjak.js";
import { u as ve } from "./discardConfirm-BHni0LNu.js";
import { _ as fe } from "./PageLayout.vue_vue_type_script_setup_true_lang-2f0QfweQ.js";
import { C as z, a as M } from "./catalogSetting-B_CqlQ0S.js";
import { u as ge } from "./priceTier-CYn3f8Qi.js";
const be = {
  class: "fm-typo-en-title-md-600 mr-auto"
}, ye = {
  class: "py-8 px-8 overflow-y-auto space-y-24"
}, he = {
  class: "space-y-16"
}, xe = {
  class: "flex flex-col"
}, ke = {
  class: "flex-1 relative border border-fm-color-neutral-gray-200 rounded-md p-16"
}, Se = {
  key: 0,
  class: "flex items-center select-none ml-16"
}, we = {
  class: "fm-typo-en-body-lg-400 text-fm-color-neutral-gray-300"
}, Ce = {
  class: "flex gap-8 items-center justify-start"
}, Te = /* @__PURE__ */ X({
  __name: "RuleGroupSideSheet",
  props: {
    modelValue: {
      type: Boolean
    },
    editingGroup: {}
  },
  emits: ["update:modelValue", "submit"],
  setup(J, {
    emit: n
  }) {
    const x = J, G = n, {
      t: i
    } = ee(), T = Z(), l = C({
      name: "",
      description: "",
      rules: []
    }), {
      convertCategoryIdToName: b,
      convertCategoryNameToId: R
    } = de({
      menuItems: $(() => T.state.menu.modules.item || []),
      categories: $(() => T.categories || [])
    }), m = $({
      get: () => x.modelValue,
      set: (s) => G("update:modelValue", s)
    }), V = C(null);
    j(() => x.editingGroup, (s) => {
      s ? l.value = {
        name: s.name,
        description: s.description,
        rules: s.rules.map((e) => ({
          ...e,
          restaurantRule: e.restaurantRule ? {
            restaurantAttributes: E(e.restaurantRule.restaurantAttributes),
            restaurantCustomAttributes: E(e.restaurantRule.restaurantCustomAttributes)
          } : void 0,
          itemRule: e.itemRule ? {
            itemAttributes: b(E(e.itemRule.itemAttributes)),
            itemCustomAttributes: E(e.itemRule.itemCustomAttributes)
          } : void 0
        }))
      } : l.value = {
        name: "",
        description: "",
        rules: []
      }, V.value = K.cloneDeep(l.value);
    }, {
      immediate: !0
    }), j(() => x.modelValue, (s) => {
      s || (l.value = {
        name: "",
        description: "",
        rules: []
      });
    });
    function S(s) {
      return `${s}_${(/* @__PURE__ */ new Date()).toISOString()}`;
    }
    async function U() {
      var e;
      const s = {
        _id: ((e = x.editingGroup) == null ? void 0 : e._id) ?? S("group"),
        name: l.value.name,
        description: l.value.description,
        rules: l.value.rules.map((a) => ({
          _id: a._id,
          restaurantRule: a.restaurantRule ? {
            restaurantAttributes: P(a.restaurantRule.restaurantAttributes),
            restaurantCustomAttributes: P(a.restaurantRule.restaurantCustomAttributes)
          } : void 0,
          itemRule: a.itemRule ? {
            itemAttributes: P(R(a.itemRule.itemAttributes)),
            itemCustomAttributes: P(a.itemRule.itemCustomAttributes)
          } : void 0
        }))
      };
      G("submit", s);
    }
    const w = () => ({
      _id: S("rule"),
      restaurantRule: {
        restaurantAttributes: null,
        restaurantCustomAttributes: null
      },
      itemRule: {
        itemAttributes: null,
        itemCustomAttributes: null
      }
    });
    function D(s) {
      l.value.rules = l.value.rules.filter((e) => e._id !== s);
    }
    function I() {
      m.value = !1;
    }
    const v = ve(() => !K.isEqual(V.value, l.value), () => {
      m.value = !1;
    });
    function c(s) {
      l.value.rules.map((a) => a._id).includes(s.menuRule._id) ? l.value.rules = l.value.rules.map((a) => a._id === s.menuRule._id ? s.menuRule : a) : l.value.rules.push({
        ...s.menuRule
      });
    }
    const p = C();
    return (s, e) => {
      const a = g("fm-button"), _ = g("FmTextField"), k = g("FmButton"), F = g("FmFormGroup"), f = g("fm-side-sheet");
      return d(), L(f, {
        modelValue: m.value,
        "onUpdate:modelValue": e[6] || (e[6] = (o) => m.value = o),
        "is-container": t(te).FmForm,
        "is-container-props": {
          onValidationSuccess: U,
          blameFormChildOnValidationFailed: !0
        },
        "max-width": 760,
        "onOn:clickedAway": t(v)
      }, {
        "side-sheet-header": y(() => [u(a, {
          variant: t(N).Tertiary,
          class: "cursor-pointer mr-4",
          "prepend-icon": "arrow_back",
          onClick: e[0] || (e[0] = () => m.value = !1)
        }, null, 8, ["variant"]), r("p", be, A(s.editingGroup ? t(i)("menu.rule_groups.edit_group") : t(i)("menu.rule_groups.create_group")), 1)]),
        "side-sheet-footer": y(() => [r("div", Ce, [u(a, {
          label: t(i)("common.save"),
          size: "lg",
          type: "submit",
          variant: "primary"
        }, null, 8, ["label"]), u(a, {
          label: t(i)("common.cancel"),
          size: "lg",
          variant: "tertiary",
          onClick: I
        }, null, 8, ["label"])])]),
        default: y(() => [r("div", ye, [u(Q, {
          title: t(i)("menu.rule_groups.form.basic_info")
        }, {
          default: y(() => [r("div", he, [u(_, {
            label: t(i)("menu.rule_groups.form.name"),
            "label-mark": "required",
            modelValue: l.value.name,
            "onUpdate:modelValue": e[1] || (e[1] = (o) => l.value.name = o),
            placeholder: t(i)("menu.rule_groups.form.name_placeholder"),
            required: "",
            rules: [(o) => !!(o != null && o.trim()) || t(i)("menu.rule_groups.validation.name_required")]
          }, null, 8, ["label", "modelValue", "placeholder", "rules"]), u(_, {
            label: t(i)("menu.rule_groups.form.description"),
            "label-mark": "optional",
            modelValue: l.value.description,
            "onUpdate:modelValue": e[2] || (e[2] = (o) => l.value.description = o),
            placeholder: t(i)("menu.rule_groups.form.description_placeholder"),
            multiline: "",
            rows: 3
          }, null, 8, ["label", "modelValue", "placeholder"])])]),
          _: 1
        }, 8, ["title"]), u(F, {
          tag: "div",
          "model-value": l.value.rules,
          rules: [(o) => o.length > 0 || t(i)("menu.rule_groups.validation.at_least_one_rule_required")]
        }, {
          default: y(() => [u(Q, {
            title: t(i)("menu.rule_groups.form.rules"),
            "sub-title": t(i)("menu.rule_groups.form.rules_description")
          }, {
            default: y(() => [l.value.rules.length > 0 ? (d(!0), h(q, {
              key: 0
            }, Y(l.value.rules, (o, O) => (d(), h(q, {
              key: O
            }, [r("div", xe, [r("div", ke, [u(pe, {
              "model-value": o,
              title: `${t(i)("menu.rule_groups.condition_view.rule")} ${O + 1}`,
              "onUpdate:modelValue": e[3] || (e[3] = (H) => c(H)),
              onDeleteRule: e[4] || (e[4] = (H) => D(H))
            }, null, 8, ["model-value", "title"])])]), l.value.rules.length > 1 && O < l.value.rules.length - 1 ? (d(), h("div", Se, [r("span", we, A(t(i)("menu.rule_groups.condition_view.or")), 1)])) : B("", !0)], 64))), 128)) : (d(), L(ae, {
              key: 1,
              title: "",
              subtitle: t(i)("menu.rule_groups.condition_view.empty_data.no_rule_available"),
              action: {
                text: t(i)("menu.link_product.rules.add_rule"),
                callback: () => {
                  var o;
                  (o = p.value) == null || o.trigger();
                }
              }
            }, null, 8, ["subtitle", "action"])), r("div", null, [u(_e, {
              ref_key: "ruleFormRef",
              ref: p,
              "is-edit-mode": !0,
              "model-value": w(),
              "onUpdate:modelValue": e[5] || (e[5] = (o) => c(o))
            }, {
              default: y(({
                trigger: o
              }) => [l.value.rules.length > 0 ? (d(), L(k, {
                key: 0,
                variant: t(N).Primary,
                label: t(i)("menu.link_product.rules.add_rule"),
                "prepend-icon": "add",
                onClick: oe(o, ["stop"])
              }, null, 8, ["variant", "label", "onClick"])) : B("", !0)]),
              _: 1
            }, 8, ["model-value"])])]),
            _: 1
          }, 8, ["title", "sub-title"])]),
          _: 1
        }, 8, ["model-value", "rules"])])]),
        _: 1
      }, 8, ["modelValue", "is-container", "is-container-props", "onOn:clickedAway"]);
    };
  }
}), Re = {
  key: 0,
  class: "space-y-8"
}, Ve = {
  class: "flex items-center justify-between"
}, Fe = {
  class: "fm-typo-en-title-md-600"
}, Ae = {
  class: "text-fm-color-typo-secondary"
}, $e = {
  class: "flex justify-end items-center"
}, Ge = {
  class: "w-[320px]"
}, Ue = {
  key: 1,
  class: "flex-1 flex flex-col min-h-0"
}, Ie = {
  class: "flex items-center justify-between p-8"
}, Me = {
  class: "flex-1 mr-8"
}, Be = {
  class: "flex flex-1 flex-col min-h-0 overflow-scroll divide-y divide-fm-color-neutral-gray-100 pb-64"
}, Ne = ["onClick"], De = {
  class: "flex-1 space-y-4"
}, Oe = {
  class: "fm-typo-en-body-lg-600"
}, ze = {
  key: 0,
  class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary"
}, Le = /* @__PURE__ */ X({
  __name: "LinkProductRuleGroups",
  setup(J) {
    const {
      t: n
    } = ee(), x = ce(), G = ue(), i = ne(G), T = Z(), l = C(T.state.menu.ruleGroupSettings || {
      groups: []
    });
    j(() => T.state.menu.ruleGroupSettings, (e) => {
      l.value = e || {
        groups: []
      };
    }, {
      deep: !0
    });
    const b = C(!1), R = C(null), m = me(), V = $(() => m.breakpoints.value.xs), S = C(""), U = $(() => l.value.groups.filter((e) => e.name.toLowerCase().includes(S.value.toLowerCase()))), w = [{
      header: n("menu.rule_groups.table.columns.name"),
      accessorKey: "name",
      enableSorting: !0
    }, {
      header: n("menu.rule_groups.table.columns.description"),
      accessorKey: "description",
      enableSorting: !1
    }, {
      id: "actions",
      size: 100,
      cell: (e) => u("div", {
        class: "px-4 flex justify-end"
      }, [u(te.FmButton, {
        icon: "delete",
        "text-color": W.NeutralGray400,
        variant: N.Tertiary,
        onClick: (a) => {
          a.stopPropagation(), c(e.row.original._id);
        }
      }, null)])
    }];
    function D(e) {
      v(e.original);
    }
    function I() {
      R.value = null, b.value = !0;
    }
    function v(e) {
      R.value = e, b.value = !0;
    }
    function c(e) {
      const a = l.value.groups.find((_) => _._id === e);
      a && x.open({
        title: n("menu.rule_groups.delete_dialog.title"),
        message: n("menu.rule_groups.delete_dialog.message", {
          name: a.name
        }),
        primaryActions: {
          text: n("menu.common.actions.remove"),
          close: !1,
          variant: N.Destructive
        },
        secondaryActions: {
          text: n("menu.common.actions.cancel"),
          close: !0
        }
      }).onPrimary(async () => {
        const _ = K.cloneDeep(l.value);
        _.groups = _.groups.filter((k) => k._id !== e), await p(_), x.close();
      });
    }
    async function p(e) {
      await i.minor(async () => {
        await T.updateMenuSetting({
          ruleGroupSettings: e
        });
      }, {
        successMessage: n("menu.rule_groups.saved_successfully")
      });
    }
    async function s(e) {
      var k;
      const a = (k = l.value.groups) == null ? void 0 : k.findIndex((F) => F._id === e._id), _ = K.cloneDeep(l.value);
      a >= 0 ? _.groups[a] = e : _.groups.push(e), await p(_), b.value = !1;
    }
    return j(() => b.value, (e) => {
      e || (R.value = null);
    }), (e, a) => {
      const _ = g("FmButton"), k = g("FmSearch"), F = g("FmTable");
      return d(), h("div", null, [V.value ? (d(), h("div", Ue, [r("div", Ie, [r("div", Me, [u(k, {
        modelValue: S.value,
        "onUpdate:modelValue": a[1] || (a[1] = (f) => S.value = f),
        autofocus: "",
        class: "w-full",
        placeholder: t(n)("menu.rule_groups.search_placeholder")
      }, null, 8, ["modelValue", "placeholder"])]), u(_, {
        icon: "add",
        variant: t(N).Secondary,
        onClick: I
      }, null, 8, ["variant"])]), r("div", Be, [(d(!0), h(q, null, Y(U.value, (f) => (d(), h("div", {
        key: f._id,
        class: "flex flex-row py-12 px-16 cursor-pointer",
        onClick: (o) => v(f)
      }, [r("div", De, [r("p", Oe, A(f.name), 1), f.description ? (d(), h("p", ze, A(f.description), 1)) : B("", !0)]), u(_, {
        "text-color": t(W).NeutralGray400,
        variant: t(N).Tertiary,
        icon: "delete",
        onClick: oe((o) => c(f._id), ["stop"])
      }, null, 8, ["text-color", "variant", "onClick"])], 8, Ne))), 128))])])) : (d(), h("div", Re, [r("div", Ve, [r("div", null, [r("h1", Fe, A(t(n)("menu.rule_groups.title")), 1), r("p", Ae, A(t(n)("menu.rule_groups.description")), 1)]), u(_, {
        label: t(n)("menu.rule_groups.create_group"),
        icon: "add",
        onClick: I
      }, null, 8, ["label"])]), r("div", $e, [r("div", Ge, [u(k, {
        modelValue: S.value,
        "onUpdate:modelValue": a[0] || (a[0] = (f) => S.value = f),
        autofocus: "",
        class: "mb-8",
        placeholder: t(n)("menu.rule_groups.search_placeholder")
      }, null, 8, ["modelValue", "placeholder"])])]), u(se, null, {
        default: y(() => [u(F, {
          "column-defs": w,
          "row-data": U.value,
          "page-size": U.value.length,
          "hide-footer": !0,
          onRowClick: D
        }, null, 8, ["row-data", "page-size"])]),
        _: 1
      })])), u(Te, {
        modelValue: b.value,
        "onUpdate:modelValue": a[2] || (a[2] = (f) => b.value = f),
        "editing-group": R.value,
        onSubmit: s
      }, null, 8, ["modelValue", "editing-group"])]);
    };
  }
}), je = {
  class: "py-16"
}, Ee = {
  key: 0,
  class: "space-y-32 max-w-[750px] xs:p-16"
}, Pe = {
  key: 0,
  class: "pl-32 space-x-8"
}, qe = {
  key: 1
}, Ke = {
  class: "space-y-8"
}, Qe = {
  class: "flex items-center justify-between"
}, Je = {
  class: "fm-typo-en-title-md-600"
}, He = {
  class: "text-fm-color-typo-secondary"
}, We = {
  key: 2
}, at = /* @__PURE__ */ X({
  __name: "MenuSetting",
  setup(J) {
    var I;
    const {
      t: n
    } = ee(), x = Z(), G = ie(), i = ue(), T = ne(i), l = $(() => ({
      lowStock: x.state.menu.lowStockThreshold,
      taxSetting: x.state.menu.masterCatalogSetting,
      priceTierSettings: x.state.menu.priceTierSettings
    })), b = C("conf"), R = $(() => [{
      label: n("menu.menu_setting.tabs.menu_configuration"),
      value: "conf"
    }, ...x.isV4 ? [{
      label: n("menu.menu_setting.tabs.price_group"),
      value: "price-group"
    }, {
      label: n("menu.menu_setting.tabs.link_product_rule_groups"),
      value: "link-product-rule-groups"
    }] : []]), m = C(z.transformTax((I = l.value.taxSetting) == null ? void 0 : I.inclusiveTaxes));
    j(() => l.value, (v) => {
      var c;
      m.value = z.transformTax((c = v.taxSetting) == null ? void 0 : c.inclusiveTaxes);
    });
    const V = $(() => {
      if (!G.currentCountry.value) return [];
      const v = m.value.mode === M.MANUAL ? m.value.inclusive ?? [] : [];
      return re.Country.getCountry(G.currentCountry.value).taxSystems.map((p) => {
        const s = v.find((e) => e.systemCode === p.code);
        return {
          inclusive: (s == null ? void 0 : s.inclusive) || !1,
          systemCode: p.code
        };
      });
    });
    function S(v) {
      const c = m.value, p = c.mode === M.AUTO ? z.manualTax(V.value) : le(c);
      p.inclusive ?? (p.inclusive = []);
      const s = p.inclusive.findIndex((e) => e.systemCode === v.systemCode);
      s < 0 ? p.inclusive.push(v) : p.inclusive.splice(s, 1, v), m.value = p;
    }
    async function U() {
      if (l.value.lowStock !== null && l.value.lowStock !== void 0 && !Number.isInteger(l.value.lowStock))
        return;
      const v = m.value.mode === M.AUTO ? null : le(m.value.inclusive);
      await T.minor(async () => {
        const c = l.value.lowStock === null || l.value.lowStock === void 0 || l.value.lowStock === 0;
        await x.updateMenuSetting({
          masterCatalogSetting: {
            inclusiveTaxes: v
          },
          lowStockThreshold: c ? null : l.value.lowStock,
          priceTierSettings: w.priceTiers.value
        });
      }, {
        successMessage: n("menu.menu_setting.fields.low_stock.updated")
      });
    }
    const w = ge(), D = [{
      header: n("menu.price_tier.settings_table.columns.name"),
      accessorKey: "key",
      enableSorting: !1
    }, {
      id: "actions",
      size: 50,
      cell: (v) => u("div", {
        class: "px-4 flex justify-end"
      }, [u(te.FmButton, {
        icon: "delete",
        "text-color": W.NeutralGray400,
        variant: N.Tertiary,
        onClick: () => w.deletePriceTier(v.row.original.key)
      }, null)])
    }];
    return (v, c) => {
      const p = g("FmTabs"), s = g("fm-radio"), e = g("fm-radio-group"), a = g("fm-checkbox"), _ = g("FormItem"), k = g("FmStepperField"), F = g("FmButton"), f = g("FmTable");
      return d(), L(fe, {
        heading: t(n)("menu.menu_setting.title")
      }, {
        default: y(() => [u(p, {
          items: R.value,
          modelValue: b.value,
          "onUpdate:modelValue": c[0] || (c[0] = (o) => b.value = o)
        }, null, 8, ["items", "modelValue"]), r("div", je, [b.value === "conf" ? (d(), h("div", Ee, [u(Q, {
          title: "Tax setting"
        }, {
          default: y(() => [u(_, {
            label: t(n)("menu.setting.tax.title")
          }, {
            default: y(() => [u(e, {
              "model-value": m.value.mode,
              "onUpdate:modelValue": c[1] || (c[1] = (o) => {
                o === t(M).AUTO ? m.value = t(z).autoTax() : m.value = t(z).manualTax(V.value);
              })
            }, {
              default: y(() => [u(s, {
                value: t(M).AUTO,
                label: t(n)("menu.setting.tax.modes.auto.label"),
                sublabel: t(n)("menu.setting.tax.modes.auto.sublabel")
              }, null, 8, ["value", "label", "sublabel"]), u(s, {
                value: t(M).MANUAL,
                label: t(n)("menu.setting.tax.modes.manual.label"),
                sublabel: t(n)("menu.setting.tax.modes.manual.sublabel")
              }, null, 8, ["value", "label", "sublabel"])]),
              _: 1
            }, 8, ["model-value"]), m.value.mode === t(M).MANUAL ? (d(), h("div", Pe, [(d(!0), h(q, null, Y(V.value, (o) => (d(), L(a, {
              key: o.systemCode,
              "model-value": o.inclusive,
              value: !0,
              label: o.systemCode + " inclusive",
              "onUpdate:modelValue": (O) => {
                S({
                  ...o,
                  inclusive: O
                });
              }
            }, null, 8, ["model-value", "label", "onUpdate:modelValue"]))), 128))])) : B("", !0)]),
            _: 1
          }, 8, ["label"])]),
          _: 1
        }), u(Q, {
          title: t(n)("menu.menu_setting.fields.low_stock.title"),
          "sub-title": t(n)("menu.menu_setting.fields.low_stock.subtitle")
        }, {
          default: y(() => [u(k, {
            "model-value": l.value.lowStock || null,
            min: 0,
            max: 1e3,
            step: 1,
            placeholder: t(n)("menu.menu_setting.fields.low_stock.not_set"),
            "onUpdate:modelValue": c[2] || (c[2] = (o) => {
              l.value.lowStock = o;
            }),
            rules: [(o) => o === null || Number.isInteger(o) || t(n)("menu.menu_setting.fields.low_stock.must_be_integer")],
            "helper-text": t(n)("menu.menu_setting.fields.low_stock.helper_text")
          }, null, 8, ["model-value", "placeholder", "rules", "helper-text"])]),
          _: 1
        }, 8, ["title", "sub-title"]), u(F, {
          onClick: U,
          label: t(n)("common.save")
        }, null, 8, ["label"])])) : B("", !0), b.value === "price-group" ? (d(), h("div", qe, [r("div", Ke, [r("div", Qe, [r("div", null, [r("h1", Je, A(t(n)("menu.price_tier.price_group")), 1), r("p", He, A(t(n)("menu.price_tier.description")), 1)]), u(F, {
          label: t(n)("menu.price_tier.add_price_group"),
          icon: "add",
          onClick: t(w).create
        }, null, 8, ["label", "onClick"])]), u(se, null, {
          default: y(() => [u(f, {
            "column-defs": D,
            "row-data": t(w).priceTiers.value,
            "page-size": t(w).priceTiers.value.length,
            "hide-footer": !0
          }, null, 8, ["row-data", "page-size"])]),
          _: 1
        })])])) : B("", !0), b.value === "link-product-rule-groups" ? (d(), h("div", We, [u(Le)])) : B("", !0)])]),
        _: 1
      }, 8, ["heading"]);
    };
  }
});
export {
  at as default
};
