"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IssueLogger = void 0;
exports.getIssueLogPath = getIssueLogPath;
exports.formatList = formatList;
exports.formatNullable = formatNullable;
const promises_1 = require("node:fs/promises");
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const global_settings_1 = require("../core/global-settings");
const catalog_1 = require("../i18n/catalog");
const provider_resolution_1 = require("../platform/provider-resolution");
const log_text_1 = require("./log-text");
function getIssueLogPath(trackedProject, issueId, homeDirectory = node_os_1.default.homedir()) {
    return (0, global_settings_1.resolveIssueLogPath)((0, provider_resolution_1.getTrackedProjectStorageSegments)(trackedProject), issueId, homeDirectory);
}
class IssueLogger {
    path;
    locale;
    #writeChain = Promise.resolve();
    #directoryReady = false;
    constructor(trackedProject, issueId, options = {}) {
        this.path = getIssueLogPath(trackedProject, issueId, options.homeDirectory);
        this.locale = options.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    }
    appendRunStart(input) {
        const timestamp = new Date().toISOString();
        return this.appendText([
            "",
            (0, log_text_1.formatLogRunStartHeading)(timestamp),
            `${log_text_1.ISSUE_LOG_TEXT.runStart.timestamp}: ${timestamp}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.runKind}: ${input.runKind}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.issueId}: ${input.issue.id}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.issueDisplayId}: ${input.issue.displayId}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.issueUrl}: ${input.issue.url}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.issueTitle}: ${input.issue.title}`,
            `${log_text_1.ISSUE_LOG_TEXT.runStart.currentLabels}: ${(0, log_text_1.formatLogList)(input.currentLabels)}`,
            `${log_text_1.ISSUE_LOG_TEXT.field.pr}: ${(0, log_text_1.formatLogNullable)(input.pullRequestUrl)}`,
            ...(input.workerId ? [`workerId: ${input.workerId}`] : []),
            ...(input.runId ? [`runId: ${input.runId}`] : []),
            "",
        ].join("\n"));
    }
    appendSection(title, body) {
        const renderedBody = body.endsWith("\n") ? body : `${body}\n`;
        return this.appendText(`\n--- ${title} ---\n${renderedBody}`);
    }
    appendText(text) {
        this.#writeChain = this.#writeChain.then(async () => {
            if (!this.#directoryReady) {
                await (0, promises_1.mkdir)(node_path_1.default.dirname(this.path), { recursive: true });
                this.#directoryReady = true;
            }
            await (0, promises_1.appendFile)(this.path, text, "utf8");
        });
        return this.#writeChain;
    }
    async flush() {
        await this.#writeChain;
    }
}
exports.IssueLogger = IssueLogger;
function formatList(values, locale = catalog_1.DEFAULT_APP_LOCALE) {
    void locale;
    return (0, log_text_1.formatLogList)(values);
}
function formatNullable(value, locale = catalog_1.DEFAULT_APP_LOCALE) {
    void locale;
    return (0, log_text_1.formatLogNullable)(value);
}
