// Copy to `src/subscribers/inventory-item.ts` in your Medusa project.
//
// Event names verified against the installed @medusajs/utils 2.13.6
// InventoryEvents constant (dist/inventory/events.js):
//   "inventory.inventory-item.created" / ".updated" / ".deleted"
//
// These fire when an inventory item's OWN properties change (tracked
// toggled, sku/cost edited) — NOT a stock quantity change at a location
// (that's inventory-level.ts). The payload id is already the inventory item
// id, so no lookup is needed.
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework";
import { sendTenderPosWebhook } from "../lib/tender-pos-webhook";

export default async function tenderPosInventoryItemHandler({
  event: { name, data },
  container,
}: SubscriberArgs<{ id: string }>) {
  const logger = container.resolve("logger");

  await sendTenderPosWebhook({
    topic: name,
    data: { resourceType: "inventory_item", inventoryItemId: data.id },
    logger,
  });
}

export const config: SubscriberConfig = {
  event: [
    "inventory.inventory-item.created",
    "inventory.inventory-item.updated",
    "inventory.inventory-item.deleted",
  ],
  context: { subscriberId: "tender-pos-inventory-item" },
};
