import { defineComponent as ee, ref as oe, computed as c, watch as te, resolveComponent as J, createBlock as _, openBlock as d, withCtx as w, createVNode as S, createElementVNode as k, unref as s, toDisplayString as re, normalizeStyle as ae, normalizeClass as T, createElementBlock as N, Fragment as se, renderList as ne } from "vue";
import { _ as le } from "./MenuTab.vue_vue_type_script_setup_true_lang-Dtv5xe0O.js";
import { _ as ie } from "./FilterSelector.vue_vue_type_script_setup_true_lang-jDZZAdUQ.js";
import { useRouter as ue, useRoute as ce } from "vue-router";
import { _ as pe } from "./Default.vue_vue_type_script_setup_true_lang-BUIcdcLn.js";
import { u as me, s as de } from "./BaseDialog.vue_vue_type_script_setup_true_lang-B2CdeeFj.js";
import { storeToRefs as f } from "pinia";
import { b as ve, c as fe, u as ge, g as ye } from "./NavigationTab.vue_vue_type_script_setup_true_lang-lfncwvdP.js";
import { _ as be } from "./NoData.vue_vue_type_script_setup_true_lang-CDKHIlF2.js";
import { useSnackbar as _e, useBreakpoints as we } from "@feedmepos/ui-library";
import { u as Se, a as ke } from "./vue-i18n-DVWuTfed.js";
import { u as xe } from "./message-dialog-UoMWOnil.js";
import { e as he, g as Ce } from "./app-C2vt2B-i.js";
import { r as Re } from "./i18n-CI_sQ5d_.js";
import { _ as Pe } from "./SelectComponent.vue_vue_type_script_setup_true_lang-Df40itH5.js";
const De = { 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" }, Ie = /* @__PURE__ */ ee({
  __name: "ReportView",
  setup($e) {
    const { t: e, te: B } = Se(), g = ue(), v = ce(), l = _e(), { promptMessage: q } = xe(), p = me(), x = ve(), { promptLoader: y } = ke(), { breakpoints: i } = we(), { isOpenAccountingDialog: F } = f(fe()), { isOpenScheduleDialog: h } = f(ge()), { disabled: C } = f(he()), R = oe(null), P = c(
      () => {
        var r;
        return ((r = R.value) == null ? void 0 : r.reportId) === u.value;
      }
    ), j = c(() => {
      var r;
      return !!((r = t.value) != null && r.isSupportPos);
    }), {
      currentReport: t,
      overviewReport: L,
      currentReportId: u,
      isOpenDownloadDialog: V,
      isOpenEmailDialog: z,
      editingReport: U,
      canCreateCustomReport: H,
      customReportQuota: D
    } = f(p), { canUpdate: M, canDuplicate: Y } = Ce();
    te(
      () => u.value,
      async () => R.value = await ye(u.value),
      { immediate: !0 }
    );
    const G = c(() => {
      var n;
      const r = u.value === ((n = L.value) == null ? void 0 : n.id), o = [
        {
          icon: "mail",
          label: e("report.common.emailSchedule"),
          onClick: () => {
            h.value = !0;
          }
        },
        {
          icon: "article",
          label: e(r ? "report.common.unsetAsOverview" : "report.common.setAsOverview"),
          onClick: async () => {
            await y(async () => {
              if (t.value)
                try {
                  await p.updateDefaultReport(r ? null : t.value.id), l.open({
                    message: `Successfully ${r ? "unset" : "set"} ${t.value.name} as Overview`,
                    type: "success"
                  });
                } catch (m) {
                  l.open({ message: JSON.stringify(m), type: "error" });
                }
            });
          }
        },
        {
          icon: "content_copy",
          label: e("report.common.copyID"),
          onClick: () => {
            t.value && (navigator.clipboard.writeText(u.value), l.open({ message: e("report.messages.copiedReportIdToClipboard"), type: "success" }));
          }
        },
        {
          icon: "report_jobs",
          label: e("report.common.reportStatus"),
          onClick: () => {
            O();
          }
        }
      ];
      return !C.value && P.value && o.splice(1, 0, {
        icon: "download",
        label: e("report.common.accountingExport"),
        onClick: I
      }), o;
    });
    async function O() {
      x.isOpenJobDialog = !0, await x.getJobs();
    }
    function Q() {
      if (!t.value || !M(t.value)) {
        l.open({
          message: e("report.messages.noPermissionUpdateReport"),
          type: "error"
        });
        return;
      }
      U.value = JSON.parse(JSON.stringify(t.value)), g.push({
        name: "reportEditor",
        query: v.query
      });
    }
    async function W() {
      if (!t.value || !Y(t.value)) {
        l.open({
          message: e("report.messages.noPermissionDuplicateReport"),
          type: "error"
        });
        return;
      }
      if (!H.value) {
        let o = `Your current plan only support up to ${D.value} custom reports.`;
        D.value === 0 && (o = "Your current plan does not support custom report."), l.open({
          message: o,
          type: "error"
        });
        return;
      }
      if (!t.value) {
        l.open({ message: "No Widget", type: "error" });
        return;
      }
      if (await q({
        title: `${e("report.messages.saveReport")}`,
        message: e("report.messages.confirmAction", {
          action: e("report.common.duplicate"),
          name: t.value.name
        })
      }) && t.value) {
        const o = JSON.parse(JSON.stringify(t.value));
        o.editable = !0, o.id = "", o.name = `${o.name} duplicate`, o.isDefault = !1;
        try {
          await y(async () => {
            const n = await p.createOrUpdateReport(o);
            p.updateCurrentReportId(n.id), g.push({
              name: "reportView",
              params: v.params,
              query: v.query
            });
          });
        } catch (n) {
          console.log(n), l.open({ message: "Failed to clone report", type: "error" });
        }
      }
    }
    function I() {
      F.value = !0;
    }
    function K() {
      t.value && (navigator.clipboard.writeText(u.value), l.open({ message: "Copied report ID to clipboard", type: "success" }));
    }
    async function X() {
      if (t.value)
        try {
          await y(async () => {
            await de(u.value, !j.value), await p.getReports();
          });
        } catch (r) {
          console.log(r), l.open({
            message: e("report.messages.failedToAction", { action: e("report.common.update") }),
            type: "error"
          });
        }
    }
    const $ = c(() => t.value ? t.value.tiles : []), Z = c(() => {
      var n;
      let o = [
        // 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"
        // },
        !!((n = t.value) != null && n.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 && (o = [
        ...o,
        { 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 }
      ], !C.value && P.value && o.push({ label: e("report.common.accountingExport"), value: "export", isPrimary: !1 }), o = o.filter((m) => m.value !== "edit")), o;
    }), b = c(() => {
      var r;
      return Re(e, B, (r = t == null ? void 0 : t.value) == null ? void 0 : r.name, "other");
    });
    return (r, o) => {
      const n = J("FmTooltip"), m = J("FmPageHead");
      return d(), _(pe, null, {
        default: w(() => {
          var E;
          return [
            S(m, {
              title: b.value,
              "back-button": !0,
              actions: Z.value,
              "onClick:back": o[0] || (o[0] = (a) => s(g).push({ name: "reports", query: s(v).query })),
              "onClick:action": o[1] || (o[1] = (a) => {
                a === "show_in_pos" && X(), a === "duplicate" && W(), a === "download" && (V.value = !0), a === "email" && (z.value = !0), a === "edit" && Q(), a === "schedule_email" && (h.value = !0), a === "content_copy" && K(), a === "export" && I(), a === "report_jobs" && O();
              }),
              class: "remove-line-clamping"
            }, {
              title: w(() => [
                S(n, {
                  content: b.value,
                  placement: "bottom",
                  "z-index": 2e3,
                  class: "flex min-w-0 max-w-full justify-start"
                }, {
                  default: w(() => [
                    k("p", De, re(b.value), 1)
                  ]),
                  _: 1
                }, 8, ["content"])
              ]),
              _: 1
            }, 8, ["title", "actions"]),
            k("div", {
              class: T(`flex flex-col overflow-y-auto ${s(i).xs || s(i).sm ? "pb-20" : ""}`),
              style: ae({ height: `calc(100% - ${s(i).lg || s(i).md ? 72 : 64}px)` })
            }, [
              k("div", Oe, [
                S(le, {
                  "model-value": s(i).lg || s(i).md ? G.value : []
                }, null, 8, ["model-value"])
              ]),
              s(t) && ((E = $.value) == null ? void 0 : E.length) > 0 ? (d(), N("div", {
                key: 0,
                class: T(["grid grid-cols-12", s(i).sm || s(i).xs ? "px-3 gap-[16px]" : "px-24 gap-[24px]"])
              }, [
                (d(!0), N(se, null, ne($.value, (a, A) => (d(), _(ie, {
                  key: `widget_${s(u)}_${A}`,
                  "model-value": a,
                  index: A
                }, null, 8, ["model-value", "index"]))), 128))
              ], 2)) : (d(), _(be, { key: 1 }))
            ], 6)
          ];
        }),
        _: 1
      });
    };
  }
}), Ye = /* @__PURE__ */ Pe(Ie, [["__scopeId", "data-v-b926988a"]]);
export {
  Ye as default
};
