import { defineComponent as D, ref as A, computed as i, watch as H, resolveComponent as u, openBlock as S, createBlock as x, unref as c, createSlots as Y, withCtx as h, createElementVNode as N, normalizeClass as $, createVNode as m, normalizeStyle as U, Teleport as q, h as p } from "vue";
import { u as E, a as K, x as j } from "./app-Bpb4394e.js";
import { u as G, _ as J } from "./SingleColumnLayout.vue_vue_type_script_setup_true_lang-sUxHOj47.js";
import { useBreakpoints as O, useDialog as Q, useSnackbar as R, components as I } from "@feedmepos/ui-library";
import { u as X } from "./vue-i18n-C4H80O5H.js";
import { a as C } from "./date2-DpnYOzUg.js";
const ie = /* @__PURE__ */ D({
  __name: "PublishView",
  setup(Z) {
    const { t } = X(), { breakpoints: f } = O(), n = E(), g = K(), w = j(), s = A(!1), l = i(() => g.state.inventory.updatedAt ? g.state.inventory.updatedAt : null), k = G(), _ = Q(), F = R(), M = i(() => n.locationsWithInventory), d = i(() => f.value.xs || f.value.sm), L = i(() => d.value ? 10 : 20);
    H(
      () => n._locations,
      async () => {
        var e;
        s.value = !0, (!n._locationWithInventory || ((e = n._locationWithInventory) == null ? void 0 : e.length) === 0) && await n.readLocationsWithInventory(), s.value = !1;
      },
      { immediate: !0 }
      // Optional: triggers the watcher immediately on mount
    );
    const P = i(() => [
      {
        label: t("inventory.common.refresh"),
        value: "refresh",
        isPrimary: !1
      },
      { label: t("inventory.publish.actions.publishAll"), value: "publish", isPrimary: !0 }
    ]);
    async function V(e) {
      switch (e) {
        case "publish":
          _.open({
            title: t("inventory.common.publish"),
            message: "Are you sure you want to publish to all locations?",
            primaryActions: { text: t("inventory.common.publish"), close: !0 },
            secondaryActions: { text: "Cancel", close: !0 }
          }).onPrimary(async () => {
            await w.publishAllInventory(), await n.readLocationsWithInventory();
          });
          break;
        case "refresh":
          s.value = !0, await n.readLocationsWithInventory(), s.value = !1;
          break;
      }
    }
    const y = A(!1), B = [
      {
        value: "publish",
        label: t("inventory.publish.actions.publishAll")
      }
    ];
    function W(e) {
      switch (y.value = !1, e) {
        case "publish":
          console.log("publish all");
      }
    }
    const z = [
      {
        id: "type",
        header: () => t("inventory.publish.columns.type"),
        enableSorting: !0,
        cell(e) {
          return p(I.FmChip, {
            color: "primary",
            label: e.row.original.type
          });
        }
      },
      {
        accessorKey: "name",
        header: () => t("inventory.publish.columns.location"),
        enableSorting: !0,
        size: 300
      },
      {
        id: "version",
        header: () => t("inventory.publish.columns.version"),
        enableSorting: !0,
        cell(e) {
          var o, a;
          return (o = e.row.original.inventory) != null && o.updatedAt ? C((a = e.row.original.inventory) == null ? void 0 : a.updatedAt) : "";
        }
      },
      {
        id: "status",
        header: () => t("inventory.publish.columns.status"),
        enableSorting: !0,
        cell(e) {
          var r;
          if (!l.value)
            return "";
          const o = ((r = e.row.original.inventory) == null ? void 0 : r.updatedAt) == l.value, a = e.row.original.dbName, v = e.row.original.type === "warehouse";
          return p(
            "div",
            {
              class: "flex items-center gap-8"
            },
            [
              p(
                "div",
                {
                  class: [
                    "fm-status-badge",
                    o ? "fm-status-badge-success-secondary" : "fm-status-badge-info-secondary"
                  ]
                },
                t(o ? "inventory.publish.status.published" : "inventory.publish.status.outdated")
              ),
              !o && !v ? p(I.FmButton, {
                class: "tertiary",
                variant: "secondary",
                icon: "sync",
                onClick: () => {
                  _.open({
                    title: t("inventory.common.publish"),
                    message: `Are you sure you want to publish this location "${e.row.original.name}"?`,
                    primaryActions: { text: t("inventory.common.publish"), close: !0 },
                    secondaryActions: { text: "Cancel", close: !0 }
                  }).onPrimary(async () => {
                    await w.publishInventory(a.split("_")[1]), await n.readLocations(), F.open({ message: "Publish request submitted", type: "success" });
                  });
                }
              }) : null
            ]
          );
        }
      }
      // {
      //   id: 'action',
      //   header: '',
      //   cell(props) {
      //     return createContextMenuComponents(
      //       [DefaultRowActions[RowActionEnum.Edit], DefaultRowActions[RowActionEnum.Delete]],
      //       (action) => {
      //         onActionMenu(action, props.row.original)
      //       }
      //     )
      //   },
      //   enableSorting: false,
      //   size: 40,
      //   meta: {
      //     cellClass: '',
      //     headerClass: ''
      //   }
      // }
    ];
    return (e, o) => {
      const a = u("FmChip"), v = u("FmTable"), r = u("FmCollapsibleTabs"), T = u("FmBottomSheet");
      return S(), x(J, {
        title: c(t)("inventory.publish.title"),
        actions: P.value,
        "onClick:action": V
      }, Y({
        default: h(() => [
          N("div", {
            class: $([
              "flex flex-col gap-8 max-h-full",
              {
                "p-0": d.value,
                "px-24 ": !d.value
              }
            ])
          }, [
            m(v, {
              style: U(c(k).tableHeight),
              "column-defs": z,
              "row-data": M.value,
              loading: !c(n)._currentLocation || s.value,
              "loading-text": "Loading",
              "page-size": L.value
            }, null, 8, ["style", "row-data", "loading", "page-size"])
          ], 2),
          (S(), x(q, { to: "body" }, [
            m(T, {
              "dismiss-away": "",
              modelValue: y.value,
              "onUpdate:modelValue": o[1] || (o[1] = (b) => y.value = b)
            }, {
              default: h(() => [
                m(r, {
                  class: "pb-8",
                  items: B,
                  "onUpdate:modelValue": o[0] || (o[0] = (b) => W(b))
                })
              ]),
              _: 1
            }, 8, ["modelValue"])
          ]))
        ]),
        _: 2
      }, [
        l.value ? {
          name: "headerInfo",
          fn: h(() => [
            m(a, {
              variant: "info",
              label: `Latest: ${c(C)(l.value)}`
            }, null, 8, ["label"])
          ]),
          key: "0"
        } : void 0
      ]), 1032, ["title", "actions"]);
    };
  }
});
export {
  ie as default
};
