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/modules/OpenData/utils.ts
var utils_exports = {};
__export(utils_exports, {
  computeAvailability: () => computeAvailability
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_getDateIsInSchedule = require("../Schedule/getDateIsInSchedule");
function toBoolean(value) {
  if (typeof value === "boolean")
    return value;
  if (typeof value === "string")
    return value === "true" || value === "1";
  if (typeof value === "number")
    return value !== 0;
  return false;
}
function resolveMessage(...candidates) {
  for (const val of candidates) {
    if (!val)
      continue;
    if (typeof val === "string" && val.length > 0)
      return val;
    if (typeof val === "object" && val !== null && !Array.isArray(val))
      return val;
  }
  return null;
}
function computeAvailability(params) {
  const { config, scheduleList } = params;
  if (!config) {
    return { available: true, reason: null, message: null };
  }
  if (config["basic.enable"] === false) {
    return {
      available: false,
      reason: "shop_disabled",
      message: resolveMessage(
        config["basic.unavailable_message"],
        config["availability.closed_message"]
      )
    };
  }
  if (toBoolean(config["availability.paused"])) {
    return {
      available: false,
      reason: "paused",
      message: resolveMessage(
        config["availability.pause_message"],
        config["availability.closed_message"]
      )
    };
  }
  const operatingHourIds = config["availability.operating_hours"];
  if (Array.isArray(operatingHourIds) && operatingHourIds.length > 0 && (scheduleList == null ? void 0 : scheduleList.length)) {
    const relevantSchedules = scheduleList.filter(
      (s) => operatingHourIds.includes(s.id)
    );
    if (relevantSchedules.length > 0) {
      const now = (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
      const isOpen = (0, import_getDateIsInSchedule.getDateIsInSchedule)(now, relevantSchedules);
      if (!isOpen) {
        return {
          available: false,
          reason: "closed",
          message: resolveMessage(config["availability.closed_message"])
        };
      }
    }
  }
  if (config["reservation.enable"] === false) {
    return {
      available: false,
      reason: "reservation_disabled",
      message: resolveMessage(config["availability.closed_message"])
    };
  }
  return { available: true, reason: null, message: null };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  computeAvailability
});
