"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DomindsPersistenceFileError = void 0;
exports.findDomindsPersistenceFileError = findDomindsPersistenceFileError;
class DomindsPersistenceFileError extends Error {
    code = 'DOMINDS_PERSISTENCE_FILE_ERROR';
    source;
    operation;
    format;
    filePath;
    eofLike;
    lineNumber;
    cause;
    constructor(args) {
        super(args.message);
        this.name = 'DomindsPersistenceFileError';
        this.source = args.source;
        this.operation = args.operation;
        this.format = args.format;
        this.filePath = args.filePath;
        this.eofLike = args.eofLike;
        this.lineNumber = args.lineNumber;
        this.cause = args.cause;
    }
}
exports.DomindsPersistenceFileError = DomindsPersistenceFileError;
function isObjectLike(value) {
    return typeof value === 'object' && value !== null;
}
function readNestedError(value) {
    if (typeof value === 'object' &&
        value !== null &&
        'error' in value &&
        typeof value.error === 'object' &&
        value.error !== null) {
        return value.error;
    }
    return undefined;
}
function readCause(value) {
    if (value instanceof Error) {
        const withCause = value;
        return withCause.cause;
    }
    if (typeof value === 'object' && value !== null && 'cause' in value) {
        return value.cause;
    }
    return undefined;
}
function findDomindsPersistenceFileError(error) {
    const queue = [error];
    const visited = new Set();
    while (queue.length > 0) {
        const current = queue.shift();
        if (current instanceof DomindsPersistenceFileError) {
            return current;
        }
        if (!isObjectLike(current)) {
            continue;
        }
        if (visited.has(current)) {
            continue;
        }
        visited.add(current);
        const nestedError = readNestedError(current);
        if (nestedError !== undefined) {
            queue.push(nestedError);
        }
        const cause = readCause(current);
        if (cause !== undefined) {
            queue.push(cause);
        }
    }
    return undefined;
}
