import { defineComponent as q, mergeModels as z, useModel as $, computed as G, resolveComponent as b, createBlock as S, unref as n, openBlock as V, withCtx as w, createElementVNode as i, createVNode as m, toDisplayString as J, createTextVNode as Q, ref as T, h as g, createElementBlock as X, Fragment as Y } from "vue";
import { useRouter as Z, useRoute as ee } from "vue-router";
import { I as R, u as E, e as N, f as oe, o as P, p as te } from "./global-loader-CbmVhJAK.js";
import { useBreakpoints as K, useSnackbar as ae, components as le, FmButtonColorThemeVariant as M } from "@feedmepos/ui-library";
import { _ as ne } from "./SelectComponent.vue_vue_type_script_setup_true_lang-N2Lg1Vpt.js";
import { storeToRefs as O } from "pinia";
import { u as re } from "./message-dialog-UoMWOnil.js";
import { _ as se } from "./NoData.vue_vue_type_script_setup_true_lang-CdFXBqlX.js";
import { _ as ie } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const me = { class: "flex flex-col" }, de = { class: "fm-typo-en-body-lg-600" }, ue = { class: "flex gap-2 overflow-hidden flex-col" }, pe = { class: "flex gap-2 overflow-hidden" }, ce = { class: "flex flex-col w-full" }, fe = { class: "flex flex-col gap-5 p-6" }, ge = { class: "flex" }, ve = /* @__PURE__ */ q({
  __name: "AddIntegrationDialog",
  props: {
    modelValue: { type: Boolean, default: !1 },
    modelModifiers: {},
    form: {
      default: () => ({ name: "", platform: R.AutoCount })
    },
    formModifiers: {}
  },
  emits: /* @__PURE__ */ z(["confirm"], ["update:modelValue", "update:form"]),
  setup(B, { emit: F }) {
    const { t: u } = E(), p = $(B, "modelValue"), s = $(B, "form"), { breakpoints: _ } = K(), { options: k } = O(N()), c = G(
      () => k.value.map((o) => ({
        label: o.name,
        value: o.platform,
        sublabel: u(`report.integration.type.${o.type}`)
      }))
    ), x = F;
    function v() {
      p.value = !1;
    }
    function y() {
      x("confirm");
    }
    return (o, e) => {
      const I = b("FmTextField"), A = b("FmSelect"), C = b("FmButton"), h = b("FmDialog"), D = b("FmBottomSheet");
      return n(_).lg || n(_).md ? (V(), S(h, {
        key: 0,
        modelValue: p.value,
        "onUpdate:modelValue": e[2] || (e[2] = (r) => p.value = r),
        overlay: !0
      }, {
        "dialog-header": w(() => [
          i("div", me, [
            i("p", de, J(n(u)("report.integration.add")), 1)
          ])
        ]),
        default: w(() => [
          i("div", ue, [
            m(I, {
              modelValue: s.value.name,
              "onUpdate:modelValue": e[0] || (e[0] = (r) => s.value.name = r),
              label: n(u)("report.common.name"),
              placeholder: n(u)("report.integration.enter")
            }, null, 8, ["modelValue", "label", "placeholder"]),
            m(A, {
              modelValue: s.value.platform,
              "onUpdate:modelValue": e[1] || (e[1] = (r) => s.value.platform = r),
              label: n(u)("report.common.platform"),
              items: c.value
            }, null, 8, ["modelValue", "label", "items"])
          ])
        ]),
        "dialog-footer": w(() => [
          e[6] || (e[6] = i("div", { class: "grow" }, null, -1)),
          m(C, {
            label: "Cancel",
            variant: "tertiary",
            onClick: v
          }),
          m(C, {
            label: "Add",
            variant: "primary",
            disabled: s.value.name === "",
            onClick: y
          }, null, 8, ["disabled"])
        ]),
        _: 1
      }, 8, ["modelValue"])) : (V(), S(D, {
        key: 1,
        modelValue: p.value,
        "onUpdate:modelValue": e[5] || (e[5] = (r) => p.value = r)
      }, {
        "bottom-sheet-header": w(() => e[7] || (e[7] = [
          Q("Integration")
        ])),
        default: w(() => [
          i("div", pe, [
            i("div", ce, [
              i("div", fe, [
                m(I, {
                  modelValue: s.value.name,
                  "onUpdate:modelValue": e[3] || (e[3] = (r) => s.value.name = r),
                  label: "Name",
                  placeholder: "Enter a name"
                }, null, 8, ["modelValue"]),
                m(ne, {
                  modelValue: s.value.platform,
                  "onUpdate:modelValue": e[4] || (e[4] = (r) => s.value.platform = r),
                  label: "Integration",
                  items: c.value
                }, null, 8, ["modelValue", "items"])
              ]),
              i("div", ge, [
                e[8] || (e[8] = i("div", { class: "grow" }, null, -1)),
                m(C, {
                  label: "Cancel",
                  variant: "tertiary",
                  onClick: v
                }),
                m(C, {
                  label: "Add Integration",
                  variant: "primary",
                  onClick: y
                })
              ])
            ])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]));
    };
  }
}), ye = { class: "gap-24 px-24 py-16" }, be = /* @__PURE__ */ q({
  __name: "Integrations",
  setup(B) {
    const F = Z(), u = ee(), { settings: p, options: s, disabled: _ } = O(N()), { getSettings: k } = N(), c = ae(), { promptLoader: x } = oe(), v = T(!1), y = T({ name: "", platform: R.AutoCount }), { t: o } = E(), e = G(() => p.value.reduce((t, a) => {
      const f = s.value.find((l) => l.platform === a.platform.name);
      return f && t.push({
        id: a._id,
        name: a.name,
        platform: f.platform,
        asset: f.asset,
        type: f.type
      }), t;
    }, []));
    async function I() {
      await x(async () => {
        await k();
      });
    }
    async function A() {
      await x(async () => {
        try {
          const t = await P({
            name: y.value.name,
            platform: {
              name: y.value.platform
            }
          });
          await k(), v.value = !1, F.push({
            name: "integration",
            params: { id: t._id },
            query: u.query
          });
        } catch (t) {
          c.open({ type: "error", message: t.message });
        }
      });
    }
    const { promptMessage: C } = re();
    async function h(t) {
      t === "new" ? v.value = !0 : t ? F.push({
        name: "integration",
        params: { id: t },
        query: u.query
      }) : c.open({ type: "error", message: o("report.integration.error.invalid") });
    }
    const { FmButton: D, FmMenu: r, FmMenuItem: H, FmIcon: _e, FmChip: L } = le, { breakpoints: U } = K(), W = [
      {
        accessorKey: "name",
        header: o("report.common.name"),
        meta: {
          maxWidth: "50px",
          width: "50px"
        },
        enableSorting: !1
      },
      {
        accessorKey: "tag",
        header: o("report.common.platform"),
        cell: (t) => {
          const a = t.row.original;
          return g("div", { class: "flex flex-row items-center gap-x-16" }, [
            g("img", { src: a.asset, class: "w-[60px]" }),
            g(
              L,
              {
                class: "text-fm-color-typo-info fm-typo-en-body-sm-600",
                compact: !0
              },
              { default: () => o(`report.integration.type.${a.type}`) }
            )
          ]);
        },
        enableSorting: !1
      },
      {
        accessorKey: "",
        id: "Action",
        header: "",
        meta: {
          maxWidth: "20px"
        },
        enableSorting: !1,
        cell: (t) => {
          const a = t.row.original, f = [
            {
              label: o("report.common.duplicate"),
              onClick: async () => {
                await x(async () => {
                  try {
                    const l = p.value.find((d) => d._id === a.id);
                    l && (await P({
                      ...l,
                      name: `${l == null ? void 0 : l.name} cloned`
                    }), await k());
                  } catch (l) {
                    c.open({ type: "error", message: l.message });
                  }
                });
              },
              class: ""
            },
            {
              label: o("report.common.delete"),
              onClick: async () => {
                await C({
                  message: o("report.integration.delete", { name: a.name }),
                  title: o("report.common.delete")
                }) && await x(async () => {
                  try {
                    await te(a.id), await I();
                  } catch (d) {
                    c.open({ type: "error", message: d.message });
                  }
                });
              },
              class: "text-fm-color-typo-error"
            }
          ];
          return g(
            "div",
            {
              class: `flex ${U.value.lg || U.value.md ? "justify-end" : ""} items-center`
            },
            {
              default: () => [
                g(
                  r,
                  { stopClickPropagation: !0, shift: !0 },
                  {
                    default: () => f.map(
                      (l) => g(
                        H,
                        {
                          onClick: l.onClick,
                          label: l.label,
                          class: l.class
                        },
                        {
                          label: () => g("p", l.label)
                        }
                      )
                    ),
                    "menu-button": () => g(
                      D,
                      {
                        variant: "tertiary",
                        size: "md",
                        icon: "more_vert",
                        iconColor: M.NeutralGray400
                      },
                      {}
                    )
                  }
                )
              ]
            }
          );
        }
      }
    ];
    function j(t) {
      _.value ? c.open({ message: o("report.integration.error.planNotSupported"), type: "error" }) : t === "create" && h("new");
    }
    return (t, a) => {
      const f = b("FmPageHead"), l = b("FmTable");
      return V(), X(Y, null, [
        m(ve, {
          modelValue: v.value,
          "onUpdate:modelValue": a[0] || (a[0] = (d) => v.value = d),
          form: y.value,
          "onUpdate:form": a[1] || (a[1] = (d) => y.value = d),
          onConfirm: A
        }, null, 8, ["modelValue", "form"]),
        m(f, {
          title: n(o)("report.integration.title"),
          class: "h-[68px]",
          actions: [
            {
              label: n(o)("report.integration.add"),
              value: "create",
              isPrimary: !0,
              bgColor: n(_) ? n(M).NeutralGray100 : void 0,
              fontColor: n(_) ? n(M).NeutralGray200 : void 0,
              prependIcon: "add"
            }
          ],
          "onClick:action": j
        }, null, 8, ["title", "actions"]),
        i("div", ye, [
          e.value.length > 0 ? (V(), S(l, {
            key: 0,
            ref: "table",
            "row-data": e.value,
            "column-defs": W,
            "shrink-at": !1,
            onRowClick: a[2] || (a[2] = (d) => {
              h(d.original.id);
            })
          }, null, 8, ["row-data"])) : (V(), S(se, {
            key: 1,
            title: n(o)("report.integration.noData.title"),
            "sub-title": n(o)("report.integration.noData.description")
          }, null, 8, ["title", "sub-title"]))
        ])
      ], 64);
    };
  }
}), Be = /* @__PURE__ */ ie(be, [["__scopeId", "data-v-8133bb2c"]]);
export {
  Be as default
};
