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";
async function loadPicoCodingOpenDataSnapshot(engineCtx, BIZ) {
  var _a, _b, _c, _d, _e, _f;
  const emptyResult = {
    picoCodingOpenData: null,
    reservationRuleProduct: null,
    reservationAssociatedMenuListIds: []
  };
  if (!engineCtx || !BIZ) return emptyResult;
  const res = await (0, import_formResourcePage.getPicoCodingOpenData)(
    {
      shop_id: (_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"),
      data: {
        scope: SCOPE,
        section_code: ["reservation", "fulfillment", "menu"],
        target: `${BIZ}+${CHANNEL}`
      }
    },
    {
      prefix: false,
      headers: {
        "Tenant-Id": (_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")
      }
    }
  );
  if ((res == null ? void 0 : res.code) !== 200) return emptyResult;
  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
  };
}
function usePicoCodingOpenData(engineCtx, BIZ) {
  const [picoCodingOpenData, setPicoCodingOpenData] = (0, import_react.useState)(null);
  const [reservationRuleProduct, setReservationRuleProduct] = (0, import_react.useState)(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
});
