"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = Logger;
const IocdWdioServiceError_1 = require("../../utils/error/IocdWdioServiceError");
/**
 * Logger(name)
 * -------------
 * Factory for creating prefixed @wdio/logger instances.
 *
 * - Dynamically loads @wdio/logger (ESM-only) inside a CJS context.
 * - Returns a Proxy immediately — methods no-op until the import resolves.
 * - Adds `.force()` method to always log to stdout regardless of WDIO log level.
 */
let factoryPromise;
function Logger(name) {
    if (!factoryPromise) {
        factoryPromise = Function('m', 'return import(m)')('@wdio/logger')
            //@ts-expect-error
            .then(mod => (n) => mod.default(n))
            .catch((err) => {
            throw new IocdWdioServiceError_1.IocdWdioServiceError(`Failed to load @wdio/logger: ${err.message}`);
        });
    }
    let logger;
    factoryPromise.then(factory => {
        logger = factory(name);
    });
    return new Proxy({}, {
        get(_t, prop) {
            if (prop === 'force') {
                // Always log directly to stdout, bypassing WDIO level filtering
                return (...args) => {
                    const ts = new Date().toISOString();
                    const grayTs = `\x1b[90m${ts}\x1b[0m`; // gray color
                    const prefix = `${grayTs} ${name}:`;
                    console.log(prefix, ...args);
                };
            }
            if (!logger) {
                // Before async import resolves, no-op instead of crashing
                return () => { };
            }
            const value = logger[prop];
            if (typeof value !== 'function') {
                throw new IocdWdioServiceError_1.IocdWdioServiceError(`Property "${String(prop)}" is not a logger method`);
            }
            return value.bind(logger);
        },
    });
}
