"use strict";
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const utils = __importStar(require("@applitools/utils"));
const logger_1 = require("@applitools/logger");
const LogHandler_1 = require("./input/LogHandler");
class Logger {
    _makeLogger() {
        return (0, logger_1.makeLogger)({
            handler: this._options.handler && this._options.handler instanceof LogHandler_1.LogHandlerData
                ? this._options.handler.toJSON()
                : this._options.handler,
            level: this._options.show || utils.general.getEnvValue('SHOW_LOGS', 'boolean') ? 'info' : 'silent',
            format: { label: this._options.label },
        });
    }
    constructor(loggerOrOptionsOrShow = false) {
        this._options = {};
        /** @internal */
        this.isLogger = true;
        if (utils.types.isBoolean(loggerOrOptionsOrShow)) {
            return new Logger({ show: loggerOrOptionsOrShow });
        }
        else if (utils.types.has(loggerOrOptionsOrShow, ['log', 'console'])) {
            this._logger = loggerOrOptionsOrShow;
        }
        else {
            this._options = loggerOrOptionsOrShow;
        }
    }
    /** @internal */
    getLogger() {
        if (!this._logger)
            this._logger = this._makeLogger();
        return this._logger;
    }
    getLogHandler() {
        if (this._options.handler) {
            if (!utils.types.has(this._options.handler, 'type')) {
                return this._options.handler;
            }
            else if (this._options.handler.type === 'file') {
                return new LogHandler_1.FileLogHandlerData(true, this._options.handler.filename, this._options.handler.append);
            }
            else if (this._options.handler.type === 'console') {
                return new LogHandler_1.ConsoleLogHandlerData(true);
            }
        }
        return new LogHandler_1.NullLogHandlerData();
    }
    setLogHandler(handler) {
        this._options.handler = handler;
        this._options.show = true;
    }
    verbose(...messages) {
        if (!this._logger)
            this._logger = this._makeLogger();
        messages.forEach(message => this._logger.log(message));
    }
    log(...messages) {
        if (!this._logger)
            this._logger = this._makeLogger();
        messages.forEach(message => this._logger.log(message));
    }
    warn(...messages) {
        if (!this._logger)
            this._logger = this._makeLogger();
        messages.forEach(message => this._logger.warn(message));
    }
    error(...messages) {
        if (!this._logger)
            this._logger = this._makeLogger();
        messages.forEach(message => this._logger.error(message));
    }
    fatal(...messages) {
        if (!this._logger)
            this._logger = this._makeLogger();
        messages.forEach(message => this._logger.fatal(message));
    }
    open() {
        var _a;
        (_a = this._logger) === null || _a === void 0 ? void 0 : _a.open();
    }
    close() {
        var _a;
        (_a = this._logger) === null || _a === void 0 ? void 0 : _a.close();
    }
    /** @internal */
    extend(label) {
        if (this._logger)
            return new Logger(this._logger.extend({ label }));
        return new Logger({
            show: this._options.show,
            label: label !== null && label !== void 0 ? label : this._options.label,
            handler: this._options.handler,
        });
    }
}
exports.Logger = Logger;
