import { defineComponent as K, computed as i, h as u, ref as U, resolveComponent as b, createElementBlock as p, openBlock as d, createVNode as f, withCtx as g, createElementVNode as V, createCommentVNode as C, toDisplayString as j, unref as n, isRef as A, createBlock as D, Fragment as G, renderList as H } from "vue";
import { useI18n as M } from "@feedmepos/mf-common";
import { components as B, FmButtonColorThemeVariant as h, FmButtonVariant as I } from "@feedmepos/ui-library";
import { _ as O } from "./RuleView.vue_vue_type_script_setup_true_lang-DRUQOy4h.js";
import { L as $ } from "./LinkProductSideSheet-DUHaljek.js";
import { u as q } from "./search-BAPEUu1R.js";
import { u as J, ap as Q } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
const W = { class: "space-y-16" }, X = { class: "flex items-center space-x-24" }, Y = {
  key: 0,
  class: "fm-typo-en-body-lg-400 text-fm-color-typo-secondary"
}, le = /* @__PURE__ */ K({
  __name: "ApplyProduct",
  props: {
    title: {},
    linkButtonLabel: {},
    overrides: {},
    selectVariant: { type: Boolean },
    showVariant: { type: Boolean },
    editable: { type: Boolean },
    categorizedItems: {},
    items: {},
    variants: {}
  },
  emits: ["update:overrides", "row-click"],
  setup(z, { emit: F }) {
    const s = z, _ = F, L = J(), { searchKey: m, filter: w } = q(), y = i(() => s.categorizedItems || L.categorizedItems), k = i(() => Q(y.value, s.overrides)), P = i(() => k.value.reduce((t, e) => {
      if (w([e.name]))
        return t.push(e), t;
      const r = e.items.filter((a) => w([a.label]));
      return r.length && t.push({ ...e, items: r }), t;
    }, [])), R = i(() => s.showVariant || s.selectVariant);
    function S(t, e) {
      var a, l;
      const r = e ? (a = s.overrides) == null ? void 0 : a.filter((o) => !(o._id === t && o.combinationKey === e)) : (l = s.overrides) == null ? void 0 : l.filter((o) => o._id !== t);
      _("update:overrides", r ?? []);
    }
    const { t: c } = M(), E = i(
      () => (t) => [
        {
          accessorKey: "label",
          enableSorting: !1,
          header: c("menu.apply_product.table.category_count", {
            name: t.name,
            count: t.items.length
          }),
          cell: (e) => {
            var l;
            const r = e.getValue();
            return ((l = e.row.original.variant) == null ? void 0 : l.length) ? u(
              "p",
              {
                class: "flex items-center space-x-8"
              },
              [
                u("p", {}, [r]),
                u(B.FmIcon, {
                  name: "keyboard_arrow_up",
                  onClick: e.row.getToggleExpandedHandler(),
                  class: [
                    "cursor-pointer",
                    "transition-transform",
                    e.row.getIsExpanded() ? "transform rotate-180" : ""
                  ]
                })
              ]
            ) : r;
          }
        },
        {
          id: "action",
          enableSorting: !1,
          cell: (e) => {
            const r = e.row.original, a = e.row.depth;
            return s.editable ? u(
              "div",
              {
                class: "flex items-center justify-end"
              },
              u(B.FmButton, {
                prependIcon: "delete",
                variant: I.Tertiary,
                textColor: h.NeutralGray400,
                onClick: () => {
                  var o;
                  if (a > 0) {
                    const v = (o = e.row.getParentRow()) == null ? void 0 : o.original;
                    S(v.value, r.value);
                  } else
                    S(r.value);
                }
              })
            ) : null;
          }
        }
      ]
    ), x = U({}), N = (t) => t.variant, T = i(() => k.value.reduce((t, e) => t + e.items.length, 0));
    return (t, e) => {
      const r = b("fm-button"), a = b("fm-search"), l = b("fm-table");
      return d(), p("div", W, [
        f(O, { title: t.title }, {
          "append-title": g(() => [
            V("div", null, [
              t.editable ? (d(), D($, {
                key: 0,
                "categorized-items": y.value,
                "initial-value": t.overrides,
                "select-variant": t.selectVariant,
                "show-variant": R.value,
                title: n(c)("menu.apply_product.link.title"),
                onSubmit: e[0] || (e[0] = ({ items: o }) => _("update:overrides", o))
              }, {
                default: g(({ trigger: o }) => [
                  f(r, {
                    "text-color": n(h).ColorPrimary,
                    "border-color": n(h).ColorPrimary,
                    variant: n(I).Secondary,
                    label: t.linkButtonLabel || n(c)("menu.common.actions.link_product"),
                    "prepend-icon": "link",
                    onClick: o
                  }, null, 8, ["text-color", "border-color", "variant", "label", "onClick"])
                ]),
                _: 1
              }, 8, ["categorized-items", "initial-value", "select-variant", "show-variant", "title"])) : C("", !0)
            ])
          ]),
          default: g(() => [
            V("div", X, [
              t.editable ? (d(), p("p", Y, j(n(c)("menu.common.labels.product_count", { count: T.value })), 1)) : C("", !0),
              f(a, {
                modelValue: n(m),
                "onUpdate:modelValue": e[1] || (e[1] = (o) => A(m) ? m.value = o : null),
                placeholder: n(c)("menu.common.labels.search_product"),
                class: "flex-1"
              }, null, 8, ["modelValue", "placeholder"])
            ])
          ]),
          _: 1
        }, 8, ["title"]),
        (d(!0), p(G, null, H(P.value, (o) => (d(), p("div", {
          key: o.id || "",
          class: "xs:overflow-x-scroll"
        }, [
          f(l, {
            "column-defs": E.value(o),
            "expanded-state": x.value,
            "get-sub-row": N,
            "page-size": 9999,
            "row-data": o.items,
            "search-value": n(m),
            "shrink-at": !1,
            "hide-footer": "",
            "onUpdate:expandedState": e[2] || (e[2] = (v) => x.value = v)
          }, null, 8, ["column-defs", "expanded-state", "row-data", "search-value"])
        ]))), 128))
      ]);
    };
  }
});
export {
  le as _
};
