// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe-neo';
import { InjectTokens } from './dependency-injection/inject-tokens.js';
import { patchInject } from './dependency-injection/container-helper.js';
import { UserBreak } from './errors/user-break.js';
import { SilentBreak } from './errors/silent-break.js';
let ErrorHandler = class ErrorHandler {
    logger;
    constructor(logger) {
        this.logger = logger;
        this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
    }
    handle(error) {
        const error_ = this.extractBreak(error);
        if (error_ instanceof UserBreak) {
            this.handleUserBreak(error_);
        }
        else if (error_ instanceof SilentBreak) {
            this.handleSilentBreak(error_);
        }
        else {
            this.handleError(error);
            process.exitCode = 1;
        }
    }
    handleUserBreak(userBreak) {
        this.logger.showUser(userBreak.message);
    }
    handleSilentBreak(silentBreak) {
        this.logger.info(silentBreak.message);
    }
    handleError(error) {
        this.logger.showUserError(error);
    }
    /**
     * Recursively checks if an error is or is caused by a UserBreak
     * Returns the UserBreak or SilentBreak if found, otherwise false
     * @param err
     */
    extractBreak(error) {
        if (error instanceof UserBreak || error instanceof SilentBreak) {
            return error;
        }
        if (error?.cause) {
            return this.extractBreak(error.cause);
        }
        return false;
    }
};
ErrorHandler = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.SoloLogger)),
    __metadata("design:paramtypes", [Object])
], ErrorHandler);
export { ErrorHandler };
//# sourceMappingURL=error-handler.js.map