import { __awaiter, __extends, __generator } from "tslib";
import { readFile } from "node:fs/promises";
import { array, defaulted, string } from "superstruct";
import { AbstractAdapter } from "./abstract";
var fileConfig = defaulted(array(string()), []);
var AddressList = /** @class */ (function (_super) {
    __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 __awaiter(this, void 0, void 0, function () {
            var data, err_1, addresses;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, readFile(this.path, { encoding: "utf8" })];
                    case 1:
                        data = _a.sent();
                        return [3 /*break*/, 3];
                    case 2:
                        err_1 = _a.sent();
                        return [2 /*return*/, this.error(err_1)];
                    case 3:
                        if (!data) {
                            return [2 /*return*/, this.error("No addresses found")];
                        }
                        try {
                            addresses = JSON.parse(data);
                            if (!fileConfig.is(addresses)) {
                                return [2 /*return*/, this.error("Address format validation failure.")];
                            }
                        }
                        catch (err) {
                            return [2 /*return*/, this.error(err)];
                        }
                        return [2 /*return*/, Promise.resolve(addresses)];
                }
            });
        });
    };
    return AddressList;
}(AbstractAdapter));
export { AddressList };
//# sourceMappingURL=file.js.map