import { E as Error } from './unknown-97022c22.js';
import './Item-f4cbd6f4.js';
import { a as Authorization } from './index-2d6441ea.js';
import './ok-c9833bf3.js';
import './Token-ba2c5d5c.js';
import './index-c2c33860.js';
import './index-8e333ab5.js';

class Handler {
  static register(callback, type, client) {
    this.client = client;
    this.callbacks.push(callback);
    callback(this.states, this.logs, this.merchants, this.settlements, this.conversion);
    if (!Handler.timer) {
      this.download(type);
      Handler.timer = setInterval(Handler.download, 600000);
    }
  }
  static async download(type) {
    var _a, _b, _c, _d, _e;
    if (!this.downloading) {
      try {
        this.downloading = true;
        const merchants = await ((_a = Handler.client) === null || _a === void 0 ? void 0 : _a.merchant.list());
        const [authorizations, settlements, conversion] = await Promise.all([
          merchants && !Error.is(merchants)
            ? Promise.all(merchants.map(m => { var _a; return (_a = Handler.client) === null || _a === void 0 ? void 0 : _a.authorization.list(true, m.id); })).then(result => result.reduce((r, c) => (c != undefined && Array.isArray(c) && r && !Error.is(r) ? [...r, ...c] : r), []))
            : (_b = Handler.client) === null || _b === void 0 ? void 0 : _b.authorization.list(true),
          type != "test" ? (_c = Handler.client) === null || _c === void 0 ? void 0 : _c.settlement.list() : [],
          (_d = Handler.client) === null || _d === void 0 ? void 0 : _d.settlement.connection.get("currency/conversion"),
        ]);
        const logs = type == "developer" ? (_e = Handler.client) === null || _e === void 0 ? void 0 : _e.log.list() : Promise.resolve([]);
        if (!Error.is(authorizations) && authorizations && !authorizations.some(Authorization.is))
          Handler.states = authorizations;
        Handler.logs = logs;
        if (!Error.is(merchants) && typeof merchants != "string")
          Handler.merchants = merchants;
        if (!Error.is(settlements) && typeof settlements != "string")
          Handler.settlements = settlements;
        if (!Error.is(conversion) && typeof conversion != "string")
          Handler.conversion = conversion;
        Handler.callbacks.forEach(callback => callback(Handler.states, Handler.logs, Handler.merchants, Handler.settlements, Handler.conversion));
        this.downloading = false;
      }
      catch (_f) {
        this.downloading = false;
      }
    }
  }
}
Handler.downloading = false;
Handler.callbacks = [];

export { Handler as H };
