import packageJson from "../../package.json" with { type: "json" };
import { dedent } from "./dedent.js";
// ANSI color codes
const colors = {
    reset: "\x1b[0m",
    cyanBright: "\x1b[96m",
    yellowBright: "\x1b[93m",
    magenta: "\x1b[35m",
    greenBright: "\x1b[92m",
    redBright: "\x1b[91m",
    gray: "\x1b[90m",
};
// Check if colors should be disabled
const shouldDisableColors = () => {
    return Boolean(process.env.NO_COLOR);
};
// Apply color if colors are enabled
const colorize = (text, color) => {
    if (shouldDisableColors()) {
        return text;
    }
    return `${colors[color]}${text}${colors.reset}`;
};
let loggers = {};
export const createLoggerInstance = (alchemyInfo, customLogger) => {
    return (loggers[alchemyInfo.appName] ??=
        customLogger ?? createFallbackLogger(alchemyInfo));
};
export const createDummyLogger = () => {
    return {
        log: () => { },
        error: () => { },
        warn: () => { },
        warnOnce: () => { },
        task: () => { },
        exit: () => { },
    };
};
export const createFallbackLogger = (alchemyInfo) => {
    if (alchemyInfo.phase !== "read") {
        console.log(dedent `
      ${colorize("Alchemy", "cyanBright")} (v${packageJson.version})
      App: ${alchemyInfo.appName}
      Phase: ${alchemyInfo.phase}
      Stage: ${alchemyInfo.stage}
      
    `);
    }
    return {
        log: console.log,
        error: (...args) => console.error(colorize("ERROR", "redBright"), ...args),
        warn: (...args) => console.warn(colorize("WARN", "yellowBright"), ...args),
        warnOnce: (message) => {
            globalThis._ALCHEMY_WARNINGS ??= new Set();
            if (globalThis._ALCHEMY_WARNINGS.has(message))
                return;
            globalThis._ALCHEMY_WARNINGS.add(message);
            console.warn(colorize("WARN", "yellowBright"), message);
        },
        task: (_id, data) => {
            const prefix = data.prefix ? `[${data.prefix}]` : "";
            // Pad the prefix to ensure consistent alignment (12 characters total including brackets)
            const paddedPrefix = prefix.padEnd(12);
            const prefixWithColor = data.prefixColor && prefix
                ? colorize(paddedPrefix, data.prefixColor)
                : paddedPrefix;
            const resource = data.resource ? colorize(data.resource, "gray") : "";
            const message = data.message;
            if (prefixWithColor && resource) {
                console.log(`${prefixWithColor}${resource} ${message}`);
            }
            else if (prefixWithColor) {
                console.log(`${prefixWithColor}${message}`);
            }
            else {
                console.log(`${message}`);
            }
        },
        exit: () => { },
    };
};
export const formatFQN = (fqn) => fqn.split("/").slice(2).join(" > ");
//# sourceMappingURL=cli.js.map