"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorMixin = void 0;
const abi_1 = require("@ethersproject/abi");
const common_1 = require("@bosonprotocol/common");
const base_core_sdk_1 = require("./../mixins/base-core-sdk");
class ErrorMixin extends base_core_sdk_1.BaseCoreSDK {
    parseError(error) {
        return Object.assign(Object.assign({}, error), { decoded: this.recurseParseError(error) });
    }
    recurseParseError(error) {
        if (this.isValidError(error)) {
            return this.decodeError(error["data"]);
        }
        if (error["data"] && error["data"]["originalError"]) {
            return this.recurseParseError(error["data"]["originalError"]);
        }
        if (error["error"]) {
            return this.recurseParseError(error["error"]);
        }
        return undefined;
    }
    isValidError(error) {
        return error["data"] && /^0x[0-9a-fA-F]{8}$/.test(error["data"]);
    }
    decodeError(data) {
        this.parseAbis();
        const error = this._errorsMap.get(data.toLowerCase());
        return error === null || error === void 0 ? void 0 : error.name;
    }
    parseAbis() {
        if (this._errorsMap.size === 0) {
            Object.keys(common_1.abis).forEach((abi) => {
                const iface = new abi_1.Interface(common_1.abis[abi]);
                Object.keys(iface.errors).forEach((error) => {
                    const sigHash = iface.getSighash(error);
                    this._errorsMap.set(sigHash.toLowerCase(), iface.errors[error]);
                });
            });
        }
    }
}
exports.ErrorMixin = ErrorMixin;
//# sourceMappingURL=mixin.js.map