"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.LocalTestOutputWriter = void 0;
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const BaseArtifactWriter_1 = require("./BaseArtifactWriter");
class LocalTestOutputWriter {
    constructor(outputDir) {
        this.outputDir = outputDir;
    }
    write(name, _contentType, data) {
        const localPath = `${this.outputDir}/${name}`;
        this.maybeCreateDirectory(localPath);
        fs.writeFileSync(localPath, data);
        return Promise.resolve(localPath);
    }
    writeObjectAsJson(name, data, replacer, reviver) {
        const localPath = `${this.outputDir}/${name}`;
        this.maybeCreateDirectory(localPath);
        if (replacer || reviver) {
            data = JSON.parse(JSON.stringify(data, replacer), reviver);
        }
        fs.writeFileSync(localPath, JSON.stringify(data));
        return Promise.resolve(localPath);
    }
    writeObjectAsJsonLines(name, data, replacer) {
        const localPath = `${this.outputDir}/${name}`;
        this.maybeCreateDirectory(localPath);
        const jsonLines = data
            .map((obj) => JSON.stringify(obj, replacer))
            .join('\n');
        fs.writeFileSync(localPath, jsonLines);
        return Promise.resolve(localPath);
    }
    maybeCreateDirectory(localPath) {
        const directory = path_1.default.dirname(localPath);
        if (!fs.existsSync(directory)) {
            fs.mkdirSync(directory, { recursive: true });
        }
    }
}
exports.LocalTestOutputWriter = LocalTestOutputWriter;
class CloudOutputWriter extends BaseArtifactWriter_1.BaseArtifactWriter {
    constructor(projectId, workspaceId, applicationId, planId, planRunId, testRunId) {
        super(projectId, workspaceId);
        this.applicationId = applicationId;
        this.planId = planId;
        this.planRunId = planRunId;
        this.testRunId = testRunId;
    }
    async writeObjectAsJson(name, data, replacer, reviver) {
        if (replacer || reviver) {
            data = JSON.parse(JSON.stringify(data, replacer), reviver);
        }
        return this.write(name, 'application/json', Buffer.from(JSON.stringify(data)));
    }
    async writeObjectAsJsonLines(name, data, replacer) {
        const jsonLines = data
            .map((obj) => JSON.stringify(obj, replacer))
            .join('\n');
        return this.write(name, 'application/jsonl', Buffer.from(jsonLines));
    }
    async write(name, contentType, data) {
        const gcsPath = this.buildGcsPath(name);
        await this.writeToGcs(gcsPath, contentType, data);
        return `${this.bucketName()}/${gcsPath}`;
    }
    buildGcsPath(name) {
        const directoryPath = `${this.applicationId}/${this.planId}/${this.planRunId}/${this.testRunId}`;
        const randomComponent = this.randomPathComponent();
        const basePath = `${BaseArtifactWriter_1.EXECUTION_OUTPUT_PATH_PREFIX}/${randomComponent}/${directoryPath}/`;
        return this.truncatePathIfNeeded(basePath, name);
    }
}
exports.default = CloudOutputWriter;
