"use strict";
// SPDX-License-Identifier: Apache-2.0
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractsErrorMapper = void 0;
const BaseError_1 = __importStar(require("../../../../../../core/error/BaseError"));
const ClearingActivated_1 = require("../ClearingActivated");
const InsufficientAllowance_1 = require("../InsufficientAllowance");
const InvalidPartition_1 = require("../InvalidPartition");
const InsufficientBalance_1 = require("../InsufficientBalance");
const SecurityPaused_1 = require("../SecurityPaused");
const codes_1 = require("../codes");
const ethers_1 = require("ethers");
const ZeroAddressNotAllowed_1 = require("../ZeroAddressNotAllowed");
const AccountBlocked_1 = require("../AccountBlocked");
const ComplianceNotAllowed_1 = require("../ComplianceNotAllowed");
const InvalidKycStatus_1 = require("../InvalidKycStatus");
const WalletRecovered_1 = require("../WalletRecovered");
const AddressNotVerified_1 = require("../AddressNotVerified");
class ContractsErrorMapper {
    static mapError(errorCode, reason) {
        const normalizedErrorCode = errorCode.toLowerCase();
        const normalizedReason = reason.toLowerCase().slice(0, 10);
        const combinedKey = `${normalizedErrorCode}:${normalizedReason}`;
        const errorFactory = this.ERROR_MAPPINGS.get(combinedKey) || this.ERROR_MAPPINGS.get(normalizedErrorCode);
        return (errorFactory?.() ||
            new BaseError_1.default(BaseError_1.ErrorCode.Unexpected, `Unexpected error code: ${errorCode} with reason: ${reason}`));
    }
    static createSelector(signature) {
        return ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(signature)).slice(0, 10);
    }
    static SELECTORS = {
        zeroAddressNotAllowed: this.createSelector("ZeroAddressNotAllowed()"),
        accountIsBlocked: this.createSelector("AccountIsBlocked(address)"),
        tokenIsPaused: this.createSelector("TokenIsPaused()"),
        clearingIsActivated: this.createSelector("ClearingIsActivated()"),
        complianceNotAllowed: this.createSelector("ComplianceNotAllowed()"),
        invalidKycStatus: this.createSelector("InvalidKycStatus()"),
        addressNotVerified: this.createSelector("AddressNotVerified()"),
        walletRecovered: this.createSelector("WalletRecovered()"),
        insufficientAllowance: this.createSelector("InsufficientAllowance(address,address)"),
        invalidPartition: this.createSelector("InvalidPartition(address,bytes32)"),
        insufficientBalance: this.createSelector("InsufficientBalance(address,uint256,uint256,bytes32)"),
    };
    static ERROR_MAPPINGS = new Map([
        [`${codes_1.EIP1066_CODES.PAUSED}:${ContractsErrorMapper.SELECTORS.tokenIsPaused}`, () => new SecurityPaused_1.SecurityPaused()],
        [`${codes_1.EIP1066_CODES.PAUSED}`, () => new SecurityPaused_1.SecurityPaused()],
        [
            `${codes_1.EIP1066_CODES.NOT_FOUND_UNEQUAL_OR_OUT_OF_RANGE}:${ContractsErrorMapper.SELECTORS.zeroAddressNotAllowed}`,
            () => new ZeroAddressNotAllowed_1.ZeroAddressNotAllowed(),
        ],
        [
            `${codes_1.EIP1066_CODES.NOT_FOUND_UNEQUAL_OR_OUT_OF_RANGE}:${ContractsErrorMapper.SELECTORS.accountIsBlocked}`,
            () => new AccountBlocked_1.AccountBlocked(),
        ],
        [
            `${codes_1.EIP1066_CODES.UNAVAILABLE}:${ContractsErrorMapper.SELECTORS.clearingIsActivated}`,
            () => new ClearingActivated_1.ClearingActivated(),
        ],
        [
            `${codes_1.EIP1066_CODES.DISALLOWED_OR_STOP}:${ContractsErrorMapper.SELECTORS.accountIsBlocked}`,
            () => new AccountBlocked_1.AccountBlocked(),
        ],
        [
            `${codes_1.EIP1066_CODES.DISALLOWED_OR_STOP}:${ContractsErrorMapper.SELECTORS.complianceNotAllowed}`,
            () => new ComplianceNotAllowed_1.ComplianceNotAllowed(),
        ],
        [
            `${codes_1.EIP1066_CODES.DISALLOWED_OR_STOP}:${ContractsErrorMapper.SELECTORS.invalidKycStatus}`,
            () => new InvalidKycStatus_1.InvalidKycStatus(),
        ],
        [
            `${codes_1.EIP1066_CODES.DISALLOWED_OR_STOP}:${ContractsErrorMapper.SELECTORS.addressNotVerified}`,
            () => new AddressNotVerified_1.AddressNotVerified(),
        ],
        [
            `${codes_1.EIP1066_CODES.REVOKED_OR_BANNED}:${ContractsErrorMapper.SELECTORS.walletRecovered}`,
            () => new WalletRecovered_1.WalletRecovered(),
        ],
        [
            `${codes_1.EIP1066_CODES.INSUFFICIENT_FUNDS}:${ContractsErrorMapper.SELECTORS.insufficientAllowance}`,
            () => new InsufficientAllowance_1.InsufficientAllowance(),
        ],
        [
            `${codes_1.EIP1066_CODES.INSUFFICIENT_FUNDS}:${ContractsErrorMapper.SELECTORS.invalidPartition}`,
            () => new InvalidPartition_1.InvalidPartition(),
        ],
        [
            `${codes_1.EIP1066_CODES.INSUFFICIENT_FUNDS}:${ContractsErrorMapper.SELECTORS.insufficientBalance}`,
            () => new InsufficientBalance_1.InsufficientBalance(),
        ],
        [
            `${codes_1.EIP1066_CODES.SUCCESS}`,
            () => new BaseError_1.default(BaseError_1.ErrorCode.Unexpected, "Unexpected success code in error context"),
        ],
    ]);
}
exports.ContractsErrorMapper = ContractsErrorMapper;
