import { defineComponent as J, ref as T, computed as p, resolveComponent as g, createBlock as k, openBlock as c, unref as l, withCtx as _, createElementVNode as d, createVNode as m, createCommentVNode as Q, toDisplayString as P, createElementBlock as M, Fragment as A, renderList as R, h as f, withModifiers as q } from "vue";
import { useI18n as H } from "@feedmepos/mf-common";
import { components as W, useBreakpoints as te, useSnackbar as oe, useDialog as ne } from "@feedmepos/ui-library";
import { _ as ae } from "./ResponsiveFabButton.vue_vue_type_script_setup_true_lang-Cn-3MRBO.js";
import { _ as le } from "./app-D-J2cjak.js";
import { I as j, J as re, M as Y, A as V, u as se, Q as ie, G as ue, a5 as de } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { _ as G } from "./RuleView.vue_vue_type_script_setup_true_lang-DRUQOy4h.js";
import { u as ce } from "./discardConfirm-BHni0LNu.js";
import { L as me } from "./LinkProductSideSheet-DUHaljek.js";
import { _ as pe } from "./PageLayout.vue_vue_type_script_setup_true_lang-2f0QfweQ.js";
const _e = { class: "fm-typo-en-title-md-600" }, fe = { class: "space-y-32" }, ve = { class: "space-y-16" }, ye = { class: "flex items-center gap-8" }, ge = { class: "fm-typo-en-body-lg-400 text-fm-color-typo-secondary" }, he = { class: "flex-1" }, be = { class: "grow overflow-y-auto" }, ke = { class: "text-fm-color-typo-secondary bg-fm-color-neutral-gray-100 px-16 py-8 border-b border-b-fm-color-neutral-gray-200" }, xe = { class: "px-16 py-8 flex-1" }, Fe = { class: "py-8" }, Ie = { class: "flex gap-8 items-center justify-start" }, Ce = /* @__PURE__ */ J({
  __name: "PrintDepartmentDialog",
  props: {
    categorizedItems: {
      type: Array,
      required: !0
    },
    canApplyItems: {
      type: Boolean,
      default: !0
    }
  },
  emits: ["onSave"],
  setup(v, { expose: t, emit: x }) {
    const N = v;
    t({
      showModal: O,
      hideModal: S
    });
    function w() {
      return {
        _id: "",
        name: "",
        applyToItems: []
      };
    }
    const F = T(!1), h = T(void 0);
    function O(s) {
      h.value = s ? j(s) : w(), r.value = j(h.value), F.value = !0;
    }
    function S() {
      F.value = !1;
    }
    const r = T(w());
    function U({ items: s }) {
      r.value = {
        ...r.value,
        applyToItems: s.map((o) => o._id)
      };
    }
    function L(s) {
      r.value = {
        ...r.value,
        applyToItems: r.value.applyToItems.filter((o) => o !== s)
      };
    }
    const { t: a } = H(), z = p(() => r.value._id ? a("menu.print_route.department_dialog.update.title") : a("menu.print_route.department_dialog.add.title")), $ = p(() => r.value._id ? a("menu.print_route.department_dialog.update.action") : a("menu.print_route.department_dialog.add.action")), D = p(() => r.value.applyToItems.length), I = p(
      () => r.value.applyToItems.reduce((s, o) => (s[o] = [], s), {})
    ), C = T(""), B = p(() => {
      const s = C.value.toLowerCase();
      return N.categorizedItems.reduce((o, b) => {
        const E = b.items.filter(
          (i) => (!s || i.name.toLowerCase().includes(s)) && !!I.value[i._id]
        );
        return E.length && o.push({ ...b, items: E }), o;
      }, []);
    }), e = x, n = ce(
      () => !re.isEqual(h.value, r.value),
      () => {
        F.value = !1;
      }
    );
    function y() {
      S(), e("onSave", r.value);
    }
    return (s, o) => {
      const b = g("FmTextField"), E = g("FmSpacer"), i = g("FmButton"), X = g("FmSearch"), Z = g("FmSideSheet");
      return c(), k(Z, {
        modelValue: F.value,
        "onUpdate:modelValue": o[3] || (o[3] = (u) => F.value = u),
        header: z.value,
        "max-width": 560,
        "is-container": l(W).FmForm,
        "is-container-props": {
          onValidationSuccess: y,
          blameFormChildOnValidationFailed: !0,
          class: "flex flex-col h-full"
        },
        "onOn:clickedAway": l(n)
      }, {
        "side-sheet-header": _(() => [
          d("span", _e, P(z.value), 1)
        ]),
        "side-sheet-footer": _(() => [
          d("div", Ie, [
            m(i, {
              label: $.value,
              size: "lg",
              variant: "primary",
              type: "submit"
            }, null, 8, ["label"]),
            m(i, {
              label: l(a)("menu.print_route.department_dialog.actions.cancel"),
              size: "lg",
              variant: "tertiary",
              onClick: S
            }, null, 8, ["label"])
          ])
        ]),
        default: _(() => [
          d("div", fe, [
            m(G, {
              title: l(a)("menu.print_route.department_dialog.sections.route_detail.title")
            }, {
              default: _(() => [
                m(b, {
                  autofocus: "",
                  modelValue: r.value.name,
                  "onUpdate:modelValue": o[0] || (o[0] = (u) => r.value.name = u),
                  rules: [
                    (u) => !!u || l(a)("menu.print_route.department_dialog.sections.route_detail.name.required")
                  ],
                  label: l(a)("menu.print_route.department_dialog.sections.route_detail.name.label"),
                  placeholder: l(a)("menu.print_route.department_dialog.sections.route_detail.name.placeholder")
                }, null, 8, ["modelValue", "rules", "label", "placeholder"])
              ]),
              _: 1
            }, 8, ["title"]),
            v.canApplyItems ? (c(), k(G, {
              key: 0,
              title: l(a)("menu.print_route.department_dialog.sections.link_product.title")
            }, {
              "append-title": _(() => [
                m(E),
                m(me, {
                  "categorized-items": v.categorizedItems,
                  "initial-value": r.value.applyToItems.map((u) => ({
                    _id: u
                  })),
                  onSubmit: o[1] || (o[1] = (u) => U(u))
                }, {
                  default: _(({ trigger: u }) => [
                    m(i, {
                      icon: "link",
                      label: l(a)("menu.scheduler.dialog.sections.products.link"),
                      variant: "secondary",
                      "text-color": "primary",
                      "border-color": "primary",
                      onClick: u
                    }, null, 8, ["label", "onClick"])
                  ]),
                  _: 1
                }, 8, ["categorized-items", "initial-value"])
              ]),
              default: _(() => [
                d("div", ve, [
                  d("div", ye, [
                    d("div", ge, P(l(a)("menu.print_route.department_dialog.sections.link_product.products_linked", {
                      count: D.value
                    })), 1),
                    d("div", he, [
                      m(X, {
                        modelValue: C.value,
                        "onUpdate:modelValue": o[2] || (o[2] = (u) => C.value = u),
                        placeholder: l(a)("menu.print_route.department_dialog.sections.link_product.search")
                      }, null, 8, ["modelValue", "placeholder"])
                    ])
                  ]),
                  d("div", be, [
                    (c(!0), M(A, null, R(B.value, (u, ee) => (c(), M("div", {
                      key: u.id || ee
                    }, [
                      d("div", ke, P(u.name), 1),
                      (c(!0), M(A, null, R(u.items, (K) => (c(), M("div", {
                        key: K._id,
                        class: "flex items-center border-b border-b-fm-color-neutral-gray-100"
                      }, [
                        d("div", xe, P(K.name), 1),
                        d("div", Fe, [
                          m(i, {
                            icon: "delete",
                            variant: "tertiary",
                            "text-color": "neutral-gray-400",
                            onClick: () => L(K._id)
                          }, null, 8, ["onClick"])
                        ])
                      ]))), 128))
                    ]))), 128))
                  ])
                ])
              ]),
              _: 1
            }, 8, ["title"])) : Q("", !0)
          ])
        ]),
        _: 1
      }, 8, ["modelValue", "header", "is-container", "is-container-props", "onOn:clickedAway"]);
    };
  }
});
function Ve(v, t) {
  return {
    module: V.F_MENU_MODULE_V4_KEY.enum.printDepartment,
    operation: Y.F_OPERATION_TYPE.create,
    data: V.FdoMenuPrintDepartment.parse(v),
    link: t.map((x) => ({
      module: V.F_MENU_MODULE_V4_KEY.enum.item,
      id: x
    }))
  };
}
function we(v, t) {
  return {
    module: V.F_MENU_MODULE_V4_KEY.enum.printDepartment,
    operation: Y.F_OPERATION_TYPE.update,
    data: V.FdoMenuPrintDepartment.parse(v),
    link: t.map((x) => ({
      module: V.F_MENU_MODULE_V4_KEY.enum.item,
      id: x
    }))
  };
}
function Ee(v) {
  return {
    module: V.F_MENU_MODULE_V4_KEY.enum.printDepartment,
    operation: Y.F_OPERATION_TYPE.delete,
    data: v._id
  };
}
const Te = {
  class: "flex flex-1 justify-end"
}, Pe = {
  class: "flex justify-end items-center"
}, Me = {
  class: "grow space-y-4"
}, Se = {
  class: "fm-typo-en-body-lg-600"
}, Le = {
  class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary"
}, Ye = /* @__PURE__ */ J({
  __name: "PrintRoute",
  setup(v) {
    const {
      t
    } = H(), x = te(), N = p(() => x.breakpoints.value.xs), w = se(), F = p(() => w.categorizedItems), h = p(() => {
      const e = w.state.menu.modules.item;
      return w.printDepartments.map((y) => ({
        ...y,
        applyToItems: e.reduce((s, o) => {
          var b;
          return (b = o.print) != null && b.department.includes(y._id) && s.push(o), s;
        }, [])
      }), []);
    }), O = p(() => h.value.length > 0), S = p(() => [{
      accessorKey: "name",
      header: () => t("menu.print_route.table.columns.name"),
      cell: (e) => e.getValue(),
      enableSorting: !1
    }, {
      accessorKey: "applyToItems",
      header: () => t("menu.print_route.table.columns.products"),
      cell: (e) => {
        const n = e.getValue();
        return t("menu.print_route.table.columns.products_count", {
          count: n.length
        });
      },
      enableSorting: !1
    }, {
      accessorKey: "_id",
      header: "",
      size: 52,
      meta: {
        cellClass: "pl-16",
        headerContentClass: "-mx-16"
      },
      enableSorting: !1,
      cell: (e) => f("div", {
        class: "flex justify-end"
      }, [f(W.FmButton, {
        icon: "delete",
        textColor: "neutral-gray-400",
        variant: "tertiary",
        onClick: q(() => {
          D(e.row.original);
        }, ["stop", "prevent"])
      })])
    }]);
    function r() {
      var e;
      a.value || (e = C.value) == null || e.showModal();
    }
    function U(e) {
      var n;
      a.value || (n = C.value) == null || n.showModal({
        ...e,
        applyToItems: e.applyToItems.map(({
          _id: y
        }) => y)
      });
    }
    const L = ie(ue(oe())), a = p(() => L.checkMenuIsLoading());
    async function z(e) {
      const n = e._id ? we(e, e.applyToItems) : Ve(e, e.applyToItems);
      await L.updateMenu(n, e.name);
    }
    const $ = ne();
    function D(e) {
      let n = [f("span", t("menu.print_route.dialogs.remove.message.prefix")), f("span", {
        class: "fm-typo-en-body-lg-600"
      }, e.name), f("span", "?")];
      e.applyToItems.length && (n = [f("span", t("menu.print_route.dialogs.remove.message.products_using")), f("span", {
        class: "fm-typo-en-body-lg-600"
      }, e.applyToItems.length), f("span", t("menu.print_route.dialogs.remove.message.products_count")), ...n, f("div", {
        class: "mt-8 fm-typo-en-body-md-400"
      }, e.applyToItems.map((y) => f("li", {
        class: "mt-4"
      }, y.name)))]), $.open({
        title: t("menu.print_route.dialogs.remove.title"),
        contentComponent: f("div", n),
        primaryActions: {
          text: t("menu.print_route.actions.remove"),
          close: !0,
          variant: "destructive"
        },
        secondaryActions: {
          text: t("menu.print_route.actions.cancel"),
          close: !0
        }
      }).onPrimary(async () => {
        await L.updateMenu(Ee(e), e.name, !0);
      });
    }
    const I = T(""), C = T(), B = p(() => {
      if (!I.value) return h.value;
      const e = I.value.toLowerCase();
      return h.value.filter((n) => n.name.toLowerCase().includes(e));
    });
    return (e, n) => {
      const y = g("FmSearch"), s = g("FmTable"), o = g("FmButton"), b = g("FmListItem"), E = g("FmList");
      return c(), k(pe, {
        heading: l(t)("menu.print_route.title")
      }, {
        "append-heading": _(() => [d("div", Te, [O.value ? (c(), k(ae, {
          key: 0,
          disabled: a.value,
          icon: "add",
          label: l(t)("menu.print_route.actions.add"),
          variant: "primary",
          onClick: r
        }, null, 8, ["disabled", "label"])) : Q("", !0)])]),
        default: _(() => [O.value ? (c(), M(A, {
          key: 1
        }, [d("div", Pe, [m(y, {
          autofocus: "",
          modelValue: I.value,
          "onUpdate:modelValue": n[0] || (n[0] = (i) => I.value = i),
          class: "my-8 w-[320px] xs:w-full xs:px-16",
          placeholder: l(t)("menu.print_route.table.search.placeholder")
        }, null, 8, ["modelValue", "placeholder"])]), N.value ? (c(), k(E, {
          key: 1,
          class: "w-full px-16 flex-1 min-h-0 overflow-scroll pb-64",
          separator: ""
        }, {
          default: _(() => [(c(!0), M(A, null, R(B.value, (i) => (c(), k(b, {
            key: i._id,
            label: i.name,
            style: {
              padding: "12px 0 !important"
            },
            onClick: () => U(i)
          }, {
            default: _(() => [d("div", Me, [d("div", Se, P(i.name), 1), d("div", Le, P(l(t)("menu.print_route.table.mobile.products_linked", {
              count: i.applyToItems.length
            })), 1)]), m(o, {
              disabled: a.value,
              icon: "delete",
              variant: "tertiary",
              "text-color": "neutral-gray-400",
              onClick: q(() => D(i), ["stop"])
            }, null, 8, ["disabled", "onClick"])]),
            _: 2
          }, 1032, ["label", "onClick"]))), 128))]),
          _: 1
        })) : (c(), k(le, {
          key: 0
        }, {
          default: _(() => [m(s, {
            "column-defs": S.value,
            loading: a.value,
            "page-size": h.value.length,
            "row-data": h.value,
            "search-value": I.value,
            shrinkAt: !1,
            "hide-footer": "",
            "pin-header-row": "",
            onRowClick: n[1] || (n[1] = (i) => U(i.original))
          }, null, 8, ["column-defs", "loading", "page-size", "row-data", "search-value"])]),
          _: 1
        }))], 64)) : (c(), k(l(de), {
          key: 0,
          action: {
            text: l(t)("menu.print_route.empty.action"),
            callback: r
          },
          subtitle: l(t)("menu.print_route.empty.subtitle"),
          title: l(t)("menu.print_route.empty.title")
        }, null, 8, ["action", "subtitle", "title"])), m(Ce, {
          ref_key: "printDepartmentRef",
          ref: C,
          "categorized-items": F.value,
          onOnSave: z
        }, null, 8, ["categorized-items"])]),
        _: 1
      }, 8, ["heading"]);
    };
  }
});
export {
  Ye as default
};
