"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemPromptController = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const SystemPromptModel_1 = require("../models/SystemPromptModel");
class SystemPromptController {
    constructor() {
        this.systemPromptModel = new SystemPromptModel_1.SystemPromptModel();
    }
    getSystemPrompt() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.systemPromptModel.getSystemPromptString();
        });
    }
    getSystemPromptComponents() {
        return __awaiter(this, void 0, void 0, function* () {
            const prefixInstruction = yield this.systemPromptModel.formatPrefixInstruction();
            const suffixInstruction = yield this.systemPromptModel.formatSuffixInstruction();
            const injectionInstruction = yield this.systemPromptModel.formatInjectionInstruction();
            const completeInstruction = yield this.systemPromptModel.getSystemPromptString();
            // const filesAndPaths = await this.systemPromptModel.getFilePathAndContents();
            return {
                prefixInstruction,
                suffixInstruction,
                injectionInstruction,
                completeInstruction,
                // filesAndPaths,
            };
        });
    }
    /**
     * @param {string[]} filePaths - list of file paths to be added (can be relative)
     * @returns {boolean[]} - list of booleans determining which has been added and which has been unsuccessfully added
     */
    addFilePaths(filePaths) {
        return __awaiter(this, void 0, void 0, function* () {
            const fileBooleanMap = filePaths.map((filePath) => __awaiter(this, void 0, void 0, function* () {
                try {
                    const absolutePath = path_1.default.resolve(filePath);
                    const existingFilePaths = this.systemPromptModel.filePaths;
                    // prevent non-file to be added and duplication
                    const isFile = yield SystemPromptController.isThisAFile(absolutePath);
                    const isDuplicate = existingFilePaths.includes(absolutePath);
                    if (!isFile || isDuplicate) {
                        return false;
                    }
                    else {
                        this.systemPromptModel.addFilePath(absolutePath);
                        return true;
                    }
                }
                catch (error) {
                    return false;
                }
            }));
            const addedStatuses = yield Promise.all(fileBooleanMap);
            return addedStatuses;
        });
    }
    removeFilePaths(filePaths) {
        return __awaiter(this, void 0, void 0, function* () {
            const existingFilePaths = this.systemPromptModel.filePaths;
            const removedStatuses = [];
            for (const filePath of filePaths) {
                const fileName = filePath.split("/").pop(); // Extract the filename from the path
                const matchingPaths = existingFilePaths.filter((path) => {
                    const pathFileName = path.split("/").pop(); // Extract the filename from the registered path
                    return pathFileName === fileName;
                });
                if (matchingPaths.length > 0) {
                    for (const matchingPath of matchingPaths) {
                        this.systemPromptModel.removeFilePath(matchingPath);
                        removedStatuses.push(true);
                    }
                }
                else {
                    removedStatuses.push(false);
                }
            }
            return removedStatuses;
        });
    }
    removeAllFilePaths() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.systemPromptModel.resetFilePaths();
        });
    }
    getFilePaths() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.systemPromptModel.filePaths;
        });
    }
    getFilePathsAndContents() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.systemPromptModel.getFilePathsAndContents();
        });
    }
    static isThisAFile(filePath) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const fileStats = yield fs_1.default.promises.stat(filePath);
                return fileStats.isFile();
            }
            catch (error) {
                // Handle any potential errors, e.g., file not found
                // console.error("Error:", error);
                return false;
            }
        });
    }
    static isThisADirectory(filePath) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const fileStats = yield fs_1.default.promises.stat(filePath);
                return fileStats.isDirectory();
            }
            catch (error) {
                // Handle any potential errors, e.g., file not found
                // console.error("Error:", error);
                return false;
            }
        });
    }
}
exports.SystemPromptController = SystemPromptController;
