import { Interface } from "@ethersproject/abi";
import { abis } from "@bosonprotocol/common";
import { BaseCoreSDK } from "./../mixins/base-core-sdk";
export class ErrorMixin extends BaseCoreSDK {
    parseError(error) {
        return { ...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?.name;
    }
    parseAbis() {
        if (this._errorsMap.size === 0) {
            Object.keys(abis).forEach((abi) => {
                const iface = new Interface(abis[abi]);
                Object.keys(iface.errors).forEach((error) => {
                    const sigHash = iface.getSighash(error);
                    this._errorsMap.set(sigHash.toLowerCase(), iface.errors[error]);
                });
            });
        }
    }
}
//# sourceMappingURL=mixin.js.map