import { defineComponent as I, ref as m, resolveComponent as p, openBlock as i, createBlock as O, withCtx as u, createVNode as y, createElementBlock as v, toDisplayString as g, createElementVNode as T, h as B } from "vue";
import { F as L } from "./FmAdminBadge-DjnBAgyv.js";
import { m as U, u as _ } from "./app-Bpb4394e.js";
import { useCoreStore as A } from "@feedmepos/mf-common";
import { useDialog as C, components as V } from "@feedmepos/ui-library";
import { u as F, a as j } from "./closing-template-ar_kUs4X.js";
const q = U((o, t) => {
  const c = _(), l = A();
  return {
    async readRequests(a, e, r) {
      const n = a ?? c.currentLocationDBName, d = `${o.inventoryBackendUrl}/${n}/purchase-request`;
      return e && r ? await t.get(`${d}?startDate=${e}&endDate=${r}`) : await t.get(d);
    },
    async readRequestById(a, e) {
      const r = e ?? c.currentLocationDBName, n = `${o.inventoryBackendUrl}/${r}/purchase-request/${a}`;
      return await t.get(n);
    },
    async readRequestByIds(a, e) {
      const r = e ?? c.currentLocationDBName, n = `${o.inventoryBackendUrl}/${r}/purchase-request/find`;
      return await t.post(n, { requestIds: a });
    },
    async getPurchaseOrders({
      start: a,
      end: e
    }) {
      const r = c.currentLocationDBName, n = `${o.inventoryBackendUrl}/${r}/purchase-order`;
      return await t.get(`${n}?start=${a}&end=${e}`);
    },
    async getLatestPurchaseOrderRequest(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/purchase-request/find-by-purchase-order-id`;
      return await t.get(`${r}/${a}`);
    },
    async getPurchaseOrderById(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/purchase-order`;
      return await t.get(`${r}/${a}`);
    },
    /**
     * @deprecated Use `processPurchaseOrderV2` instead.
     */
    async processPurchaseOrder(a, e) {
      const r = c.currentLocationDBName, n = e || r, d = `${o.inventoryBackendUrl}/${n}/purchase-order/run-operation`;
      return await t.put(d, a);
    },
    async createPurchaseOrder(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/purchase-order`;
      return await t.post(r, a);
    },
    async updatePurchaseOrder(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/purchase-order`;
      return await t.put(r, a);
    },
    async importFromXilnex(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/purchase-order/import-from-xilnex`;
      return await t.post(r, a);
    },
    async processPurchaseOrderV2(a, e) {
      const r = c.currentLocationDBName, n = e || r, d = `${o.inventoryBackendUrl}/${n}/v2/purchase-order/run-operation`;
      return await t.put(d, a);
    },
    async getWorkflowConfig() {
      var n;
      const e = `business_${(n = l.currentBusiness.value) == null ? void 0 : n._id}`, r = `${o.inventoryBackendUrl}/${e}/v2/purchase-order/workflow-config`;
      return await t.get(r);
    },
    async updateDeliveryOrder(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/delivery-order`;
      return await t.put(r, a);
    },
    async createDeliveryOrder(a) {
      const e = c.currentLocationDBName, r = `${o.inventoryBackendUrl}/${e}/delivery-order`;
      return await t.post(r, a);
    },
    async getDeliveryOrderTrails(a, e) {
      const r = c.currentLocationDBName, n = `${o.inventoryBackendUrl}/${r}/delivery-order/audit-trails`;
      return await t.post(n, { purchaseOrderId: a, deliveryOrderId: e });
    }
  };
}), P = { key: 0 }, S = { key: 1 }, K = { key: 2 }, W = /* @__PURE__ */ I({
  __name: "SystemTrailDialog",
  props: {
    purchaseOrderId: {},
    deliveryOrderId: {},
    adjustmentId: {},
    closingId: {}
  },
  setup(o) {
    const t = o, c = q(), l = F(), a = j(), e = m(!1), r = C(), n = m([]), d = m("");
    async function $() {
      t.adjustmentId ? await l.getAuditTrails(t.adjustmentId).then((s) => {
        n.value = s;
      }) : t.closingId ? await a.getAuditTrails(t.closingId).then((s) => {
        n.value = s;
      }) : t.purchaseOrderId && t.deliveryOrderId && await c.getDeliveryOrderTrails(t.purchaseOrderId, t.deliveryOrderId).then((s) => {
        n.value = s;
      });
    }
    function w(s) {
      r.open({
        title: "Metadata",
        contentComponent: B(
          "pre",
          { class: "bg-gray-100 p-4 rounded whitespace-pre-wrap" },
          JSON.stringify(s, null, 2)
        )
      });
    }
    const f = [
      {
        accessorKey: "createdAt",
        header: () => "Created At"
      },
      {
        accessorKey: "createdBy",
        header: () => "Created By",
        cell: (s) => s.getValue().name
      },
      {
        accessorKey: "message",
        header: () => "Message"
      },
      {
        accessorKey: "metadata",
        header: () => "Metadata",
        cell: (s) => B(V.FmButton, {
          label: "View",
          onClick: () => w(s.getValue())
        }),
        meta: {
          cellSpanClass: ""
        }
      }
    ];
    return (s, h) => {
      const N = p("FmButton"), D = p("FmTable"), b = p("FmDialog");
      return i(), O(b, {
        modelValue: e.value,
        "onUpdate:modelValue": h[0] || (h[0] = (k) => e.value = k),
        overlay: !0,
        dismissAway: "",
        "max-width": "800px"
      }, {
        "dialog-button": u(() => [
          y(L, null, {
            default: u(() => [
              y(N, {
                variant: "plain",
                label: "Trails",
                size: "md",
                "prepend-icon": "overview",
                onClick: $
              })
            ]),
            _: 1
          })
        ]),
        "dialog-header": u(() => [
          s.adjustmentId ? (i(), v("div", P, 'Adjustment Template "' + g(s.adjustmentId) + '" Trails', 1)) : s.closingId ? (i(), v("div", S, 'Closing Template "' + g(s.closingId) + '" Trails', 1)) : (i(), v("div", K, 'Delivery Order "' + g(s.deliveryOrderId) + '" Trails', 1))
        ]),
        default: u(() => [
          T("div", null, [
            y(D, {
              "row-data": n.value,
              "column-defs": f,
              "search-value": d.value,
              "shrink-at": !1
            }, null, 8, ["row-data", "search-value"])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
});
export {
  W as _,
  q as u
};
