/**
 * Error Parser
 * Routes errors to appropriate error classes based on step and context
 */
import { IgnoredSignatureStepError, ListAccountError, ListCurrencyError, NonceStepError, NotEnoughFunds, PayinExtraIdError, PayloadStepError, SignatureStepError, UnknownAccountError, } from "./SwapError";
/**
 * Transaction step where error occurred
 */
export var StepError;
(function (StepError) {
    StepError["NONCE"] = "NonceStepError";
    StepError["PAYLOAD"] = "PayloadStepError";
    StepError["SIGNATURE"] = "SignatureStepError";
    StepError["IGNORED_SIGNATURE"] = "IgnoredSignatureStepError";
    StepError["CHECK_FUNDS"] = "CheckFundsStepError";
    StepError["LIST_ACCOUNT"] = "ListAccountStepError";
    StepError["LIST_CURRENCY"] = "ListCurrencyStepError";
    StepError["UNKNOWN_ACCOUNT"] = "UnknownAccountStepError";
    StepError["PAYIN_EXTRA_ID"] = "PayinExtraIdStepError";
})(StepError || (StepError = {}));
/**
 * Input for error parsing
 */
export var CustomErrorType;
(function (CustomErrorType) {
    CustomErrorType["SWAP"] = "swap";
})(CustomErrorType || (CustomErrorType = {}));
/**
 * Maps step errors to error constructors
 */
const ErrorMap = {
    [StepError.NONCE]: NonceStepError,
    [StepError.PAYLOAD]: PayloadStepError,
    [StepError.SIGNATURE]: SignatureStepError,
    [StepError.IGNORED_SIGNATURE]: IgnoredSignatureStepError,
    [StepError.CHECK_FUNDS]: NotEnoughFunds,
    [StepError.LIST_ACCOUNT]: ListAccountError,
    [StepError.LIST_CURRENCY]: ListCurrencyError,
    [StepError.UNKNOWN_ACCOUNT]: UnknownAccountError,
    [StepError.PAYIN_EXTRA_ID]: PayinExtraIdError,
};
/**
 * Creates a step-specific error by wrapping the original error
 *
 * @param error - Original error that occurred
 * @param step - Step where error occurred (optional)
 * @returns Wrapped error or original error if no step specified
 */
export function createStepError({ error, step }) {
    // If no step specified, return original error
    if (!step) {
        return error;
    }
    // Get error constructor for this step
    const ErrorConstructor = ErrorMap[step];
    if (!ErrorConstructor) {
        return error;
    }
    // Wrap original error in step-specific error class
    return new ErrorConstructor(error);
}
/**
 * Parses an error to determine the correct error class to return based on the context.
 */
export function parseError({ error, step, customErrorType }) {
    if (!step || customErrorType !== CustomErrorType.SWAP) {
        return error;
    }
    return createStepError({ error, step });
}
export const hasMessage = (value) => Boolean(value && typeof value === "object" && "message" in value);
export const toError = (value) => {
    if (value instanceof Error) {
        return value;
    }
    if (hasMessage(value) && typeof value.message === "string") {
        return new Error(value.message);
    }
    return new Error(typeof value === "string"
        ? value
        : (() => {
            try {
                return JSON.stringify(value);
            }
            catch {
                return String(value);
            }
        })());
};
//# sourceMappingURL=parser.js.map