import { defineComponent as te, ref as ae, computed as p, watch as re, resolveComponent as J, createBlock as N, openBlock as S, withCtx as x, createVNode as f, createElementVNode as C, unref as l, toDisplayString as se, normalizeStyle as le, normalizeClass as B, createElementBlock as ne } from "vue";
import { _ as ie } from "./MenuTab.vue_vue_type_script_setup_true_lang-CpLX_mmh.js";
import { _ as ce } from "./TileGrid.vue_vue_type_script_setup_true_lang-jbujDyFY.js";
import { u as pe, l as ue, s as me } from "./GlobalFilterSelect.vue_vue_type_script_setup_true_lang-DXtWF7LP.js";
import { useRouter as de, useRoute as ve } from "vue-router";
import { _ as fe } from "./Default.vue_vue_type_script_setup_true_lang-8Lfk9D0S.js";
import { storeToRefs as y } from "pinia";
import { b as ye, c as ge, u as be, g as _e } from "./NavigationTab.vue_vue_type_script_setup_true_lang-TBmbqppw.js";
import { _ as we } from "./SelectComponent.vue_vue_type_script_setup_true_lang-CGbb3b5G.js";
import { useSnackbar as Se, useBreakpoints as xe } from "@feedmepos/ui-library";
import { u as Ce, a as he, _ as ke } from "./_plugin-vue_export-helper-onplcgja.js";
import { u as Te } from "./message-dialog-UoMWOnil.js";
import { e as Re, g as Pe } from "./app-dOrzXjgr.js";
import { r as De } from "./i18n-CI_sQ5d_.js";
const Ie = { class: "fm-typo-en-title-md-600 block min-w-0 max-w-full overflow-hidden text-ellipsis whitespace-nowrap" }, Oe = { class: "sticky top-0 z-10 bg-white" }, $e = /* @__PURE__ */ te({
  __name: "ReportView",
  setup(Ae) {
    const { t: e, te: q } = Ce(), g = de(), d = ve(), s = Se(), { promptMessage: h } = Te(), u = pe(), k = ye(), { promptLoader: v } = he(), { breakpoints: i } = xe(), { isOpenAccountingDialog: j } = y(ge()), { isOpenScheduleDialog: T } = y(be()), { disabled: R } = y(Re()), P = ae(null), D = p(
      () => {
        var a;
        return ((a = P.value) == null ? void 0 : a.reportId) === c.value;
      }
    ), F = p(() => {
      var a;
      return !!((a = o.value) != null && a.isSupportPos);
    }), {
      currentReport: o,
      overviewReport: V,
      currentReportId: c,
      isOpenDownloadDialog: z,
      isOpenEmailDialog: L,
      editingReport: U,
      canCreateCustomReport: H,
      customReportQuota: I
    } = y(u), { canUpdate: M, canDuplicate: W, isAdmin: Y } = Pe(), b = p(
      () => Y.value && !!o.value && !o.value.isDefault
    );
    re(
      () => c.value,
      async () => P.value = await _e(c.value),
      { immediate: !0 }
    );
    const G = p(() => {
      var r;
      const a = c.value === ((r = V.value) == null ? void 0 : r.id), t = [
        {
          icon: "mail",
          label: e("report.common.emailSchedule"),
          onClick: () => {
            T.value = !0;
          }
        },
        {
          icon: "article",
          label: e(a ? "report.common.unsetAsOverview" : "report.common.setAsOverview"),
          onClick: async () => {
            await v(async () => {
              if (o.value)
                try {
                  await u.updateDefaultReport(a ? null : o.value.id), s.open({
                    message: `Successfully ${a ? "unset" : "set"} ${o.value.name} as Overview`,
                    type: "success"
                  });
                } catch (m) {
                  s.open({ message: JSON.stringify(m), type: "error" });
                }
            });
          }
        },
        {
          icon: "content_copy",
          label: e("report.common.copyID"),
          onClick: () => {
            o.value && (navigator.clipboard.writeText(c.value), s.open({ message: e("report.messages.copiedReportIdToClipboard"), type: "success" }));
          }
        },
        {
          icon: "report_jobs",
          label: e("report.common.reportStatus"),
          onClick: () => {
            O();
          }
        }
      ];
      return !R.value && D.value && t.splice(1, 0, {
        icon: "download",
        label: e("report.common.accountingExport"),
        onClick: A
      }), b.value && t.splice(t.length - 1, 0, {
        icon: "library_add",
        label: e("report.common.copyToTemplate"),
        onClick: () => {
          $();
        }
      }), t;
    });
    async function O() {
      k.isOpenJobDialog = !0, await k.getJobs();
    }
    function Q() {
      if (!o.value || !M(o.value)) {
        s.open({
          message: e("report.messages.noPermissionUpdateReport"),
          type: "error"
        });
        return;
      }
      U.value = JSON.parse(JSON.stringify(o.value)), g.push({
        name: "reportEditor",
        query: d.query
      });
    }
    async function K() {
      if (!o.value || !W(o.value)) {
        s.open({
          message: e("report.messages.noPermissionDuplicateReport"),
          type: "error"
        });
        return;
      }
      if (!H.value) {
        let t = `Your current plan only support up to ${I.value} custom reports.`;
        I.value === 0 && (t = "Your current plan does not support custom report."), s.open({
          message: t,
          type: "error"
        });
        return;
      }
      if (!o.value) {
        s.open({ message: "No Widget", type: "error" });
        return;
      }
      if (await h({
        title: `${e("report.messages.duplicateReport")}`,
        message: e("report.messages.confirmAction", {
          action: e("report.common.duplicate"),
          name: o.value.name
        })
      }) && o.value) {
        const t = JSON.parse(JSON.stringify(o.value));
        t.editable = !0, t.id = "", t.name = `${t.name} duplicate`, t.isDefault = !1;
        try {
          await v(async () => {
            const r = await u.createOrUpdateReport(t);
            u.updateCurrentReportId(r.id), g.push({
              name: "reportView",
              params: d.params,
              query: d.query
            });
          });
        } catch (r) {
          console.log(r), s.open({ message: "Failed to clone report", type: "error" });
        }
      }
    }
    async function $() {
      var t;
      if (!b.value) {
        s.open({
          message: e("report.messages.noPermission"),
          type: "error"
        });
        return;
      }
      if (!o.value) {
        s.open({ message: "No Widget", type: "error" });
        return;
      }
      if (await h({
        title: `${e("report.common.copyToTemplate")}`,
        message: e("report.messages.confirmAction", {
          action: e("report.common.copyToTemplate"),
          name: (t = o == null ? void 0 : o.value) == null ? void 0 : t.name
        })
      }))
        try {
          await v(async () => {
            var r;
            await u.createReportTemplate(o.value), s.open({
              message: e("report.messages.copyTemplateSuccess", { name: (r = o.value) == null ? void 0 : r.name }),
              type: "success"
            });
          });
        } catch (r) {
          console.log(r), s.open({
            message: e("report.messages.failedToAction", { action: e("report.common.copyToTemplate") }),
            type: "error"
          });
        }
    }
    function A() {
      j.value = !0;
    }
    function X() {
      o.value && (navigator.clipboard.writeText(c.value), s.open({ message: "Copied report ID to clipboard", type: "success" }));
    }
    async function Z() {
      if (o.value)
        try {
          await v(async () => {
            await me(c.value, !F.value), await u.getReports();
          });
        } catch (a) {
          console.log(a), s.open({
            message: e("report.messages.failedToAction", { action: e("report.common.update") }),
            type: "error"
          });
        }
    }
    const _ = p(() => o.value ? o.value.tiles : []), ee = p(() => ue(_.value)), oe = p(() => {
      var r;
      let t = [
        // TODO: In future once decided to support report showing configuration for POS,
        // uncomment this:
        // {
        //   label: t("report.common.showInPOS"),
        //   value: "show_in_pos",
        //   isPrimary: false,
        //   appendIcon: isCurrentReportShownInPOS.value
        //     ? "radio_button_checked"
        //     : "radio_button_unchecked"
        // },
        !!((r = o.value) != null && r.editable) ? { label: e("report.common.edit"), value: "edit", isPrimary: !1 } : {
          label: e("report.common.duplicate"),
          value: "duplicate",
          isPrimary: !1
        },
        {
          label: e("report.common.download"),
          value: "download",
          isPrimary: !0,
          prependIcon: "download"
        },
        { label: e("report.schedule.sendEmail"), value: "email", isPrimary: !0 }
      ];
      return i.value.xs && (t = [
        ...t,
        { label: e("report.schedule.sendEmail"), value: "email", isPrimary: !1 },
        { label: e("report.common.reportStatus"), value: "report_jobs", isPrimary: !1 },
        { label: e("report.common.emailSchedule"), value: "schedule_email", isPrimary: !1 },
        { label: e("report.common.copyID"), value: "content_copy", isPrimary: !1 }
      ], b.value && t.push({
        label: e("report.common.copyToTemplate"),
        value: "copy_to_template",
        isPrimary: !1
      }), !R.value && D.value && t.push({ label: e("report.common.accountingExport"), value: "export", isPrimary: !1 }), t = t.filter((m) => m.value !== "edit")), t;
    }), w = p(() => {
      var a;
      return De(e, q, (a = o == null ? void 0 : o.value) == null ? void 0 : a.name, "other");
    });
    return (a, t) => {
      const r = J("FmTooltip"), m = J("FmPageHead");
      return S(), N(fe, null, {
        default: x(() => {
          var E;
          return [
            f(m, {
              title: w.value,
              "back-button": !0,
              actions: oe.value,
              class: "remove-line-clamping",
              "onClick:back": t[0] || (t[0] = (n) => l(g).push({ name: "reports", query: l(d).query })),
              "onClick:action": t[1] || (t[1] = (n) => {
                n === "show_in_pos" && Z(), n === "duplicate" && K(), n === "download" && (z.value = !0), n === "email" && (L.value = !0), n === "edit" && Q(), n === "schedule_email" && (T.value = !0), n === "content_copy" && X(), n === "copy_to_template" && $(), n === "export" && A(), n === "report_jobs" && O();
              })
            }, {
              title: x(() => [
                f(r, {
                  content: w.value,
                  placement: "bottom",
                  "z-index": 2e3,
                  class: "flex min-w-0 max-w-full justify-start"
                }, {
                  default: x(() => [
                    C("p", Ie, se(w.value), 1)
                  ]),
                  _: 1
                }, 8, ["content"])
              ]),
              _: 1
            }, 8, ["title", "actions"]),
            C("div", {
              class: B(`flex flex-col overflow-y-auto ${l(i).xs || l(i).sm ? "pb-20" : ""}`),
              style: le({ height: `calc(100% - ${l(i).lg || l(i).md ? 72 : 64}px)` })
            }, [
              C("div", Oe, [
                f(ie, {
                  "model-value": l(i).lg || l(i).md ? G.value : []
                }, null, 8, ["model-value"])
              ]),
              l(o) && ((E = _.value) == null ? void 0 : E.length) > 0 ? (S(), ne("div", {
                key: 0,
                class: B(["grid grid-cols-12", l(i).sm || l(i).xs ? "px-3 gap-[16px]" : "px-24 gap-[24px]"])
              }, [
                f(ce, {
                  tiles: _.value,
                  "report-id": l(c),
                  "leaf-index-by-id": ee.value
                }, null, 8, ["tiles", "report-id", "leaf-index-by-id"])
              ], 2)) : (S(), N(we, { key: 1 }))
            ], 6)
          ];
        }),
        _: 1
      });
    };
  }
}), Ye = /* @__PURE__ */ ke($e, [["__scopeId", "data-v-a22ca746"]]);
export {
  Ye as default
};
