'use strict';

const unknown = require('./unknown-38d3748c.js');
require('./Item-00ce8c24.js');
const index = require('./index-863a0811.js');
require('./ok-97a5fd0b.js');
require('./Token-8c5ba5d9.js');
require('./index-c910887e.js');
require('./index-0b2930ca.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 && !unknown.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 && !unknown.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 (!unknown.Error.is(authorizations) && authorizations && !authorizations.some(index.Authorization$1.is))
          Handler.states = authorizations;
        Handler.logs = logs;
        if (!unknown.Error.is(merchants) && typeof merchants != "string")
          Handler.merchants = merchants;
        if (!unknown.Error.is(settlements) && typeof settlements != "string")
          Handler.settlements = settlements;
        if (!unknown.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 = [];

exports.Handler = Handler;
