"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressList = void 0;
var tslib_1 = require("tslib");
var promises_1 = require("node:fs/promises");
var superstruct_1 = require("superstruct");
var abstract_1 = require("./abstract");
var fileConfig = (0, superstruct_1.defaulted)((0, superstruct_1.array)((0, superstruct_1.string)()), []);
var AddressList = (function (_super) {
    tslib_1.__extends(AddressList, _super);
    function AddressList(opts) {
        var _a;
        var _b = (opts !== null && opts !== void 0 ? opts : {}).path, path = _b === void 0 ? "addresses.json" : _b;
        return _super.call(this, (_a = opts === null || opts === void 0 ? void 0 : opts.name) !== null && _a !== void 0 ? _a : "fs:".concat(path), path, opts) || this;
    }
    AddressList.prototype.update = function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var data, err_1, addresses;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4, (0, promises_1.readFile)(this.path, { encoding: "utf8" })];
                    case 1:
                        data = _a.sent();
                        return [3, 3];
                    case 2:
                        err_1 = _a.sent();
                        return [2, this.error(err_1)];
                    case 3:
                        if (!data) {
                            return [2, this.error("No addresses found")];
                        }
                        try {
                            addresses = JSON.parse(data);
                            if (!fileConfig.is(addresses)) {
                                return [2, this.error("Address format validation failure.")];
                            }
                        }
                        catch (err) {
                            return [2, this.error(err)];
                        }
                        return [2, Promise.resolve(addresses)];
                }
            });
        });
    };
    return AddressList;
}(abstract_1.AbstractAdapter));
exports.AddressList = AddressList;
//# sourceMappingURL=file.js.map