var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/hooks/usePicoCodingOpenData.ts
var usePicoCodingOpenData_exports = {};
__export(usePicoCodingOpenData_exports, {
  loadPicoCodingOpenDataSnapshot: () => loadPicoCodingOpenDataSnapshot,
  usePicoCodingOpenData: () => usePicoCodingOpenData
});
module.exports = __toCommonJS(usePicoCodingOpenData_exports);
var import_react = require("react");
var import_dayjs = __toESM(require("dayjs"));
var import_associatedMenuListIdsFromOpenData = require("../data/associatedMenuListIdsFromOpenData");
var import_reservationRuleProducts = require("../data/reservationRuleProducts");
var import_formResourcePage = require("../serve/formResourcePage");
var import_productQuery = require("../serve/productQuery");
var CHANNEL = "pos";
var SCOPE = "board";
var EMPTY_PICO_CODING_OPEN_DATA_SNAPSHOT = {
  picoCodingOpenData: null,
  reservationRuleProduct: null,
  reservationAssociatedMenuListIds: []
};
var picoCodingSnapshotCache = /* @__PURE__ */ new Map();
var picoCodingSnapshotInflight = /* @__PURE__ */ new Map();
function buildPicoCodingSnapshotCacheKey(shopId, tenantId, biz) {
  return [
    String(shopId ?? ""),
    String(tenantId ?? ""),
    SCOPE,
    CHANNEL,
    biz
  ].join("::");
}
function resolvePicoCodingIdentity(engineCtx, BIZ) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const shopId = (_c = (_b = (_a = engineCtx == null ? void 0 : engineCtx.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getData) == null ? void 0 : _c.call(_b, "shop_id");
  const tenantId = (_f = (_e = (_d = engineCtx == null ? void 0 : engineCtx.appHelper) == null ? void 0 : _d.utils) == null ? void 0 : _e.getData) == null ? void 0 : _f.call(_e, "tenant_id");
  const channel = (_i = (_h = (_g = engineCtx == null ? void 0 : engineCtx.appHelper) == null ? void 0 : _g.utils) == null ? void 0 : _h.getData) == null ? void 0 : _i.call(_h, "channel");
  const biz = BIZ != null ? String(BIZ).trim() : "";
  if (!engineCtx || !shopId || !biz) return null;
  return {
    shopId,
    tenantId,
    biz,
    channel,
    cacheKey: buildPicoCodingSnapshotCacheKey(shopId, tenantId, biz)
  };
}
async function loadPicoCodingOpenDataSnapshot(engineCtx, BIZ) {
  const identity = resolvePicoCodingIdentity(engineCtx, BIZ);
  if (!identity) return EMPTY_PICO_CODING_OPEN_DATA_SNAPSHOT;
  const cached = picoCodingSnapshotCache.get(identity.cacheKey);
  if (cached) return cached;
  const inflight = picoCodingSnapshotInflight.get(identity.cacheKey);
  if (inflight) return inflight;
  const requestPromise = (async () => {
    const res = await (0, import_formResourcePage.getPicoCodingOpenData)(
      {
        shop_id: identity.shopId,
        data: {
          scope: SCOPE,
          section_code: ["reservation", "fulfillment", "menu"],
          target: `${identity.biz}+${(identity == null ? void 0 : identity.channel) || CHANNEL}`
        }
      },
      {
        prefix: false,
        headers: {
          "Tenant-Id": identity.tenantId
        }
      }
    );
    if ((res == null ? void 0 : res.code) !== 200) return EMPTY_PICO_CODING_OPEN_DATA_SNAPSHOT;
    const picoCodingOpenData = res.data;
    const reservationAssociatedMenuListIds = (0, import_associatedMenuListIdsFromOpenData.mapAssociatedMenusToMenuListIds)(picoCodingOpenData);
    const linkIds = (0, import_reservationRuleProducts.collectLinkProductIdsFromReservationRules)(
      picoCodingOpenData == null ? void 0 : picoCodingOpenData["fulfillment.enabled_resource_rules"]
    );
    if (linkIds.length === 0) {
      return {
        picoCodingOpenData,
        reservationRuleProduct: null,
        reservationAssociatedMenuListIds
      };
    }
    const now = (0, import_dayjs.default)();
    const products = await (0, import_productQuery.queryProductsByIds)({
      product_ids: linkIds,
      schedule_date: now.format("YYYY-MM-DD"),
      schedule_datetime: now.format("YYYY-MM-DD HH:mm:ss")
    });
    return {
      picoCodingOpenData,
      reservationRuleProduct: (0, import_reservationRuleProducts.pickFirstDurationProduct)(products),
      reservationAssociatedMenuListIds
    };
  })();
  picoCodingSnapshotInflight.set(identity.cacheKey, requestPromise);
  try {
    const snapshot = await requestPromise;
    picoCodingSnapshotCache.set(identity.cacheKey, snapshot);
    return snapshot;
  } finally {
    picoCodingSnapshotInflight.delete(identity.cacheKey);
  }
}
function usePicoCodingOpenData(engineCtx, BIZ) {
  const cachedSnapshot = (() => {
    const identity = resolvePicoCodingIdentity(engineCtx, BIZ);
    return identity ? picoCodingSnapshotCache.get(identity.cacheKey) : void 0;
  })();
  const [picoCodingOpenData, setPicoCodingOpenData] = (0, import_react.useState)(
    (cachedSnapshot == null ? void 0 : cachedSnapshot.picoCodingOpenData) ?? null
  );
  const [reservationRuleProduct, setReservationRuleProduct] = (0, import_react.useState)((cachedSnapshot == null ? void 0 : cachedSnapshot.reservationRuleProduct) ?? null);
  const reservationAssociatedMenuListIds = (0, import_react.useMemo)(
    () => (0, import_associatedMenuListIdsFromOpenData.mapAssociatedMenusToMenuListIds)(picoCodingOpenData),
    [picoCodingOpenData]
  );
  (0, import_react.useEffect)(() => {
    let cancelled = false;
    (async () => {
      const snapshot = await loadPicoCodingOpenDataSnapshot(engineCtx, BIZ);
      if (cancelled) return;
      setPicoCodingOpenData(snapshot.picoCodingOpenData);
      setReservationRuleProduct(snapshot.reservationRuleProduct);
    })();
    return () => {
      cancelled = true;
    };
  }, []);
  return {
    picoCodingOpenData,
    reservationRuleProduct,
    reservationAssociatedMenuListIds
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  loadPicoCodingOpenDataSnapshot,
  usePicoCodingOpenData
});
