var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/modules/OpenData/index.ts
var OpenData_exports = {};
__export(OpenData_exports, {
  OpenDataModule: () => OpenDataModule,
  computeAvailability: () => import_utils2.computeAvailability
});
module.exports = __toCommonJS(OpenData_exports);
var import_BaseModule = require("../BaseModule");
var import_utils = require("./utils");
__reExport(OpenData_exports, require("./types"), module.exports);
var import_utils2 = require("./utils");
var OpenDataModule = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    this.defaultName = "openData";
    this.defaultVersion = "1.0.0";
    this.openCache = false;
    this.otherParams = {};
  }
  async initialize(core, options) {
    var _a, _b;
    this.core = core;
    this.store = options.store;
    this.store.data = ((_a = options.initialState) == null ? void 0 : _a.data) ?? null;
    this.store.lastFetchedAt = ((_b = options.initialState) == null ? void 0 : _b.lastFetchedAt) ?? null;
    this.otherParams = options.otherParams || {};
    if (this.otherParams.cacheId) {
      this.openCache = this.otherParams.openCache ?? false;
      this.cacheId = this.otherParams.cacheId;
    }
    if (this.otherParams.fatherModule) {
      this.fatherModule = this.otherParams.fatherModule;
    }
    this.request = core.getPlugin("request");
    this.window = core.getPlugin("window");
    if (!this.request)
      throw new Error("OpenDataModule 需要 request 插件支持");
    if (!this.window)
      throw new Error("OpenDataModule 需要 window 插件支持");
    console.log("[OpenDataModule] 初始化完成");
  }
  async fetchOpenData(params) {
    var _a, _b, _c, _d;
    const shopId = (_b = (_a = this.otherParams) == null ? void 0 : _a.getStateData) == null ? void 0 : _b.call(_a, "shop_id");
    if (!shopId)
      throw new Error("[OpenDataModule] 无法获取 shop_id");
    const tenantId = (_d = (_c = this.otherParams) == null ? void 0 : _c.getStateData) == null ? void 0 : _d.call(_c, "tenant_id");
    const headers = {};
    if (tenantId)
      headers["Tenant-Id"] = String(tenantId);
    const response = await this.request.post(
      `/open/my-pisell/${shopId}/setting`,
      {
        scope: params.scope,
        target: params.target,
        section_code: params.section_code
      },
      { pisell2: true, headers }
    );
    if ((response == null ? void 0 : response.status) === false) {
      throw new Error((response == null ? void 0 : response.message) || "获取 OpenData 配置失败");
    }
    const data = (response == null ? void 0 : response.data) || {};
    this.store.data = data;
    this.store.lastFetchedAt = Date.now();
    return data;
  }
  getOpenData() {
    return this.store.data;
  }
  getLastFetchedAt() {
    return this.store.lastFetchedAt ?? null;
  }
  checkAvailability(scheduleModule) {
    var _a;
    const scheduleList = scheduleModule ? scheduleModule.getScheduleListByIds(
      ((_a = this.store.data) == null ? void 0 : _a["availability.operating_hours"]) || []
    ) : void 0;
    return (0, import_utils.computeAvailability)({
      config: this.store.data,
      scheduleList
    });
  }
  storeChange() {
    if (this.openCache) {
      this.checkSaveCache({
        cacheId: this.cacheId,
        fatherModule: this.fatherModule,
        store: this.store,
        cacheKey: ["data", "lastFetchedAt"]
      });
    }
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  OpenDataModule,
  computeAvailability,
  ...require("./types")
});
