import { defineComponent as re, computed as p, resolveComponent as g, createElementBlock as H, openBlock as _, renderSlot as Te, createVNode as n, withCtx as s, createCommentVNode as ne, createElementVNode as m, toDisplayString as y, ref as b, h as v, onMounted as Ce, createBlock as z, unref as l, normalizeStyle as ke, normalizeClass as T, withDirectives as te, Fragment as Fe, renderList as Le, withModifiers as oe, vShow as le } from "vue";
import { a as Ve } from "./SelectComponent.vue_vue_type_script_setup_true_lang-CGbb3b5G.js";
import { u as se, a as $e } from "./_plugin-vue_export-helper-onplcgja.js";
import { _ as Se } from "./Default.vue_vue_type_script_setup_true_lang-8Lfk9D0S.js";
import { u as Re } from "./message-dialog-UoMWOnil.js";
import { u as Pe } from "./GlobalFilterSelect.vue_vue_type_script_setup_true_lang-DXtWF7LP.js";
import { r as ae } from "./i18n-CI_sQ5d_.js";
import { useBreakpoints as ze, useSnackbar as De, components as Ie, FmButtonColorThemeVariant as Ne } from "@feedmepos/ui-library";
import { useRouter as Oe, useRoute as Be } from "vue-router";
import { storeToRefs as Ee } from "pinia";
import { g as Ke } from "./app-dOrzXjgr.js";
const Me = { class: "flex flex-col h-full" }, Ue = ["src"], je = { class: "fm-typo-en-body-lg-600 text-fm-color-typo-primary" }, Ae = { class: "fm-typo-en-body-sm-400 text-fm-color-typo-secondary text-center" }, He = /* @__PURE__ */ re({
  __name: "NoTemplate",
  props: {
    title: { default: "report.view.template.noTemplates" },
    subTitle: { default: "report.view.template.noTemplatesHint" },
    svgPath: { default: "" }
  },
  setup(q) {
    const { t: w, te: c } = se(), i = q;
    function o(d) {
      return c(d) ? w(d) : d;
    }
    const C = p(() => o(i.title)), D = p(() => o(i.subTitle));
    return (d, u) => {
      const $ = g("FmCard");
      return _(), H("div", Me, [
        Te(d.$slots, "menu"),
        n($, {
          class: "p-24 h-full flex flex-col gap-2 items-center justify-center",
          variant: "flat"
        }, {
          default: s(() => [
            d.svgPath ? (_(), H("img", {
              key: 0,
              src: d.svgPath,
              alt: "No data illustration",
              class: "mb-4 w-80 h-80"
            }, null, 8, Ue)) : ne("", !0),
            m("p", je, y(C.value), 1),
            m("p", Ae, y(D.value), 1)
          ]),
          _: 1
        })
      ]);
    };
  }
}), qe = { class: "flex items-center gap-2" }, Ge = { class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary" }, Je = { class: "w-[343px]" }, Qe = { class: "flex items-center gap-2" }, We = { class: "fm-typo-en-body-md-600" }, Xe = { class: "fm-typo-en-body-sm-400 text-fm-color-typo-secondary" }, Ye = { class: "fm-typo-en-title-sm-600" }, Ze = { class: "flex flex-col gap-3" }, ut = /* @__PURE__ */ re({
  __name: "Template",
  setup(q) {
    const w = Pe(), { isAdmin: c } = Ke(), { breakpoints: i } = ze(), { t: o, te: C } = se(), D = Oe(), d = Be(), u = De(), { promptLoader: $ } = $e(), { promptMessage: G } = Re(), { FmButton: I, FmMenu: N, FmMenuItem: k, FmIcon: O, FmChip: B, FmSearch: ie, FmTooltip: me } = Ie, { editingReport: ce, reportTemplates: ue, categories: pe } = Ee(w), E = p(() => pe.value ?? []), J = p(() => ue.value ?? []), K = p(() => [
      {
        label: o("report.common.all"),
        value: void 0
      },
      ...E.value.map((t) => ({
        label: M(t.name),
        value: t.id
      }))
    ]), S = b(void 0), de = p(
      () => {
        var t, e;
        return ((t = K.value.find((a) => a.value === S.value)) == null ? void 0 : t.label) ?? ((e = K.value[0]) == null ? void 0 : e.label) ?? "";
      }
    ), R = b(!1), F = b(null), x = b(""), L = b(""), V = b(""), fe = p(() => [
      {
        label: o("report.common.uncategorized"),
        value: ""
      },
      ...E.value.map((t) => ({
        label: M(t.name),
        value: t.id
      }))
    ]), P = b(""), h = p(() => {
      const t = P.value.toLowerCase(), e = o("report.common.uncategorized");
      return J.value.map((a) => {
        const f = E.value.find((j) => j.id === a.category);
        return {
          id: a.id,
          name: ae(o, C, Q(a), "other"),
          description: ae(o, C, a.description || "", "other"),
          categoryId: a.category || "",
          categoryLabel: f ? M(f.name) : e,
          isOwner: a.isOwner ?? !1
        };
      }).filter((a) => S.value && a.categoryId !== S.value ? !1 : !P.value.trim() || a.name.toLowerCase().includes(t) || a.description.toLowerCase().includes(t));
    }), ve = p(
      () => h.value.map((t) => `${t.id}${t.name}${t.categoryLabel}`).join("")
    ), ge = p(() => [
      {
        accessorKey: "name",
        header: o("report.common.name"),
        enableSorting: !1,
        cell: (t) => v("span", t.getValue())
      },
      {
        accessorKey: "categoryLabel",
        header: o("report.common.category"),
        enableSorting: !1,
        meta: {
          width: "100px"
        },
        cell: (t) => v(
          B,
          {
            class: `${t.getValue() !== o("report.common.uncategorized") ? "bg-fm-color-system-info-100 text-fm-color-typo-info" : "text-fm-color-typo-secondary"}  fm-typo-en-body-sm-600`,
            compact: !0
          },
          { default: () => t.getValue() }
        )
      },
      {
        accessorKey: "description",
        header: o("report.common.description"),
        enableSorting: !1,
        cell: (t) => v("span", t.getValue())
      },
      {
        accessorKey: "",
        id: "Action",
        header: "",
        meta: {
          width: "0px"
        },
        cell: (t) => {
          const e = t.row.original, a = c.value && !e.isOwner;
          return v(
            "div",
            {
              class: `flex ${i.value.lg || i.value.md ? "justify-end" : ""} items-center`
            },
            {
              default: () => c.value ? [
                v(
                  N,
                  {
                    stopClickPropagation: !0,
                    shift: !0,
                    disabled: a
                  },
                  {
                    default: () => [
                      v(k, {
                        label: o("report.common.edit"),
                        disabled: a,
                        onClick: () => X(e.id)
                      }),
                      v(k, {
                        label: o("report.common.delete"),
                        disabled: a,
                        onClick: () => Y(e.id)
                      })
                    ],
                    "menu-button": () => v(I, {
                      variant: "tertiary",
                      size: "md",
                      icon: "more_vert",
                      iconColor: Ne.NeutralGray400,
                      disabled: a
                    })
                  }
                )
              ] : []
            }
          );
        }
      }
    ]);
    function M(t) {
      const e = `report.category.${t}`;
      return C(e) ? o(e) : t;
    }
    function ye(t) {
      return t.id;
    }
    function Q(t) {
      return t.name;
    }
    function be(t) {
      S.value = t;
    }
    function U(t) {
      return J.value.find((e) => ye(e) === t);
    }
    function W(t) {
      const e = U(t);
      if (!e) return;
      const a = {
        ...e.report,
        id: "",
        isDefault: !1,
        editable: !0
      }, f = Q(e);
      a.name = `${f} duplicate`, ce.value = a, D.push({
        name: "reportEditor",
        query: d.query
      });
    }
    function X(t) {
      if (!c.value) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      const e = U(t);
      if (!e) return;
      if (c.value && !e.isOwner) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      const a = JSON.parse(JSON.stringify(e));
      F.value = a, x.value = a.name || "", L.value = a.description || "", V.value = a.category || "", R.value = !0;
    }
    async function Y(t) {
      if (!c.value) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      const e = U(t);
      if (!e) return;
      if (c.value && !e.isOwner) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      await G({
        title: o("report.messages.deleteTemplate"),
        message: o("report.messages.confirmDelete", { name: e.name })
      }) && await $(async () => {
        await w.removeReportTemplates(e), u.open({
          message: o("report.messages.deleteSuccess", { name: e.name }),
          type: "success"
        });
      });
    }
    function Z() {
      R.value = !1, F.value = null, x.value = "", L.value = "", V.value = "";
    }
    async function _e() {
      if (!c.value) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      if (!F.value) return;
      if (c.value && !F.value.isOwner) {
        u.open({ message: o("report.messages.noPermission"), type: "error" });
        return;
      }
      const t = o("report.common.update"), e = {
        ...F.value,
        name: x.value,
        description: L.value,
        category: V.value
      };
      if (await G({
        title: t,
        message: o("report.messages.confirmAction", {
          action: t,
          name: e.name
        })
      }))
        try {
          await $(async () => {
            await w.updateReportTemplate(e);
          }), u.open({
            message: o("report.messages.saveSuccess"),
            type: "success"
          }), Z();
        } catch (f) {
          console.error(f), u.open({
            message: o("report.messages.failedToAction", { action: o("report.common.update") }),
            type: "error"
          });
        }
    }
    return Ce(async () => {
      await w.getReportTemplates();
    }), (t, e) => {
      const a = g("FmPageHead"), f = g("FmForm"), j = g("FmListItem"), we = g("FmList"), xe = g("FmTable"), ee = g("FmTextField"), he = g("FmDialog");
      return _(), z(Se, null, {
        default: s(() => [
          n(a, {
            title: l(o)("report.view.template.title"),
            class: "h-[68px] fm-typo-en-title-md-600"
          }, {
            title: s(() => [
              m("div", qe, [
                m("p", null, y(l(o)("report.view.template.title")), 1),
                n(l(me), {
                  content: l(o)("report.view.template.sharedTooltip"),
                  placement: "bottom",
                  "z-index": 2e3
                }, {
                  default: s(() => [
                    n(l(O), {
                      name: "info",
                      size: "md",
                      color: "primary"
                    })
                  ]),
                  _: 1
                }, 8, ["content"])
              ])
            ]),
            _: 1
          }, 8, ["title"]),
          m("div", {
            class: T(`overflow-y-hidden flex flex-col gap-8 ${l(i).sm || l(i).xs ? "mb-20" : ""}`),
            style: ke({ height: `calc(100% - ${l(i).lg || l(i).md ? 72 : 64}px)` })
          }, [
            n(f, {
              class: T(`flex items-center gap-2 ${l(i).sm || l(i).xs ? "px-3" : "px-24"}`)
            }, {
              default: s(() => [
                n(l(N), null, {
                  "menu-button": s(() => [
                    n(l(B), { class: "fm-typo-en-body-md-600 text-fm-color-neutral-gray-400" }, {
                      default: s(() => [
                        m("p", null, y(de.value), 1),
                        n(l(O), { name: "expand_more" })
                      ]),
                      _: 1
                    })
                  ]),
                  default: s(() => [
                    (_(!0), H(Fe, null, Le(K.value, (r, A) => (_(), z(l(k), {
                      key: A,
                      label: r.label,
                      onClick: () => be(r.value)
                    }, null, 8, ["label", "onClick"]))), 128))
                  ]),
                  _: 1
                }),
                m("div", {
                  class: T(`flex ${l(i).sm || l(i).xs ? "justify-center items-center" : ""}`)
                }, [
                  m("p", Ge, y(l(o)("report.common.results", { count: h.value.length })), 1)
                ], 2),
                e[6] || (e[6] = m("div", { class: "grow" }, null, -1)),
                m("div", Je, [
                  n(l(ie), {
                    modelValue: P.value,
                    "onUpdate:modelValue": e[0] || (e[0] = (r) => P.value = r),
                    class: T(`${l(i).lg || l(i).md ? "grow" : ""}`)
                  }, null, 8, ["modelValue", "class"])
                ])
              ]),
              _: 1
            }, 8, ["class"]),
            te((_(), z(xe, {
              key: ve.value,
              ref: "table",
              class: T([l(i).sm || l(i).xs ? "px-0" : "px-24", "report-table"]),
              style: { height: "calc(100% - 40px)" },
              "column-defs": ge.value,
              "row-data": h.value,
              "page-size": h.value.length,
              "hide-footer": !0,
              "row-class-name": "row",
              onRowClick: e[1] || (e[1] = (r) => W(r.original.id))
            }, {
              "list-row": s(({ original: r }) => [
                n(we, {
                  onClick: () => W(r.id)
                }, {
                  default: s(() => [
                    n(j, {
                      tag: "label",
                      label: r.name,
                      clickable: ""
                    }, {
                      append: s(() => [
                        m("div", Qe, [
                          n(l(B), {
                            class: T(
                              r.categoryLabel !== l(o)("report.common.uncategorized") ? "bg-fm-color-system-info-100 text-fm-color-typo-info" : "text-fm-color-typo-secondary"
                            ),
                            compact: !0
                          }, {
                            default: s(() => [
                              m("p", We, y(r.categoryLabel), 1)
                            ]),
                            _: 2
                          }, 1032, ["class"]),
                          l(c) ? (_(), z(l(N), {
                            key: 0,
                            placement: "left",
                            "stop-click-propagation": !0
                          }, {
                            "menu-button": s(() => [
                              n(l(O), { name: "more_vert" })
                            ]),
                            default: s(() => [
                              n(l(k), {
                                label: l(o)("report.common.edit"),
                                onClick: oe((A) => X(r.id), ["stop"])
                              }, null, 8, ["label", "onClick"]),
                              n(l(k), {
                                label: l(o)("report.common.delete"),
                                class: "text-fm-color-typo-error",
                                onClick: oe((A) => Y(r.id), ["stop"])
                              }, null, 8, ["label", "onClick"])
                            ]),
                            _: 2
                          }, 1024)) : ne("", !0)
                        ])
                      ]),
                      "sub-label": s(() => [
                        m("p", Xe, y(r.description), 1)
                      ]),
                      _: 2
                    }, 1032, ["label"])
                  ]),
                  _: 2
                }, 1032, ["onClick"])
              ]),
              _: 1
            }, 8, ["class", "column-defs", "row-data", "page-size"])), [
              [le, h.value.length > 0]
            ]),
            te(n(He, {
              title: "report.view.template.noTemplates",
              "sub-title": "report.view.template.noTemplatesHint"
            }, null, 512), [
              [le, h.value.length === 0]
            ])
          ], 6),
          n(he, {
            modelValue: R.value,
            "onUpdate:modelValue": e[5] || (e[5] = (r) => R.value = r),
            overlay: !0,
            "dismiss-away": !0
          }, {
            "dialog-header": s(() => [
              m("p", Ye, y(l(o)("report.common.edit")), 1)
            ]),
            default: s(() => [
              m("div", Ze, [
                n(ee, {
                  modelValue: x.value,
                  "onUpdate:modelValue": e[2] || (e[2] = (r) => x.value = r),
                  label: l(o)("report.common.name"),
                  placeholder: l(o)("report.view.report.enter")
                }, null, 8, ["modelValue", "label", "placeholder"]),
                n(ee, {
                  modelValue: L.value,
                  "onUpdate:modelValue": e[3] || (e[3] = (r) => L.value = r),
                  label: l(o)("report.common.description"),
                  placeholder: l(o)("report.common.description")
                }, null, 8, ["modelValue", "label", "placeholder"]),
                n(Ve, {
                  modelValue: V.value,
                  "onUpdate:modelValue": e[4] || (e[4] = (r) => V.value = r),
                  label: l(o)("report.common.category"),
                  items: fe.value,
                  "disable-search": !0
                }, null, 8, ["modelValue", "label", "items"])
              ])
            ]),
            "dialog-footer": s(() => [
              e[7] || (e[7] = m("div", { class: "grow" }, null, -1)),
              n(l(I), {
                label: l(o)("report.common.cancel"),
                variant: "tertiary",
                onClick: Z
              }, null, 8, ["label"]),
              n(l(I), {
                label: l(o)("report.common.save"),
                variant: "primary",
                disabled: x.value.trim() === "",
                onClick: _e
              }, null, 8, ["label", "disabled"])
            ]),
            _: 1
          }, 8, ["modelValue"])
        ]),
        _: 1
      });
    };
  }
});
export {
  ut as default
};
