"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
exports.logRunStart = logRunStart;
const fs = __importStar(require("node:fs"));
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
const chalk_1 = __importDefault(require("chalk"));
const DEFAULT_LOG_FILENAME = 'aidev.log';
function resolveLogFile() {
    const raw = (process.env.AIDEV_LOG_PATH || '').trim();
    if (!raw)
        return path.join(process.cwd(), DEFAULT_LOG_FILENAME);
    if (raw.startsWith('~/') || raw === '~') {
        return path.join(os.homedir(), raw.slice(2));
    }
    if (path.isAbsolute(raw))
        return raw;
    return path.resolve(process.cwd(), raw);
}
function getLogFile() {
    const file = resolveLogFile();
    const dir = path.dirname(file);
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
    return file;
}
// Strip ANSI escape codes for clean file output
function strip(s) {
    return s.replace(/\x1B\[[0-9;]*m/g, '');
}
function timestamp() {
    return new Date().toISOString();
}
function writeLog(level, msg) {
    const line = `${timestamp()} [${level}] ${strip(msg)}\n`;
    fs.appendFileSync(getLogFile(), line, 'utf8');
}
function logRunStart() {
    const sep = '─'.repeat(60);
    fs.appendFileSync(getLogFile(), `\n${sep}\n${timestamp()} [run] started\n${sep}\n`, 'utf8');
}
exports.logger = {
    info: (msg) => {
        console.log(chalk_1.default.blue('[aidev]'), msg);
        writeLog('info', msg);
    },
    success: (msg) => {
        console.log(chalk_1.default.green('[aidev]'), msg);
        writeLog('success', msg);
    },
    warn: (msg) => {
        console.log(chalk_1.default.yellow('[aidev]'), msg);
        writeLog('warn', msg);
    },
    error: (msg) => {
        console.error(chalk_1.default.red('[aidev]'), msg);
        writeLog('error', msg);
    },
    task: (msg) => {
        console.log(chalk_1.default.cyan('[task]'), msg);
        writeLog('task', msg);
    },
    debug: (msg) => {
        if (process.env.DEBUG) {
            console.log(chalk_1.default.gray('[debug]'), msg);
            writeLog('debug', msg);
        }
    },
};
//# sourceMappingURL=logger.js.map