"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const scanDirectory_1 = require("../../../../lib/scanDirectory");
const storage_1 = require("../../../../lib/storage");
const getArgs_1 = require("../../../../util/getArgs");
exports.default = (app) => {
    const { Controller } = app;
    async function setWorkFolder(newWorkFolder, workFolderKey = 'workFolder') {
        const directories = await scanDirectory_1.default(newWorkFolder);
        storage_1.default.set(workFolderKey, newWorkFolder);
        return directories;
    }
    return class HomeController extends Controller {
        constructor() {
            super(...arguments);
            this.workFolder = '/';
            this.projectPath = getArgs_1.default('project');
        }
        async getWorkFolder() {
            let workFolder = '';
            if (this.projectPath) {
                workFolder = storage_1.default.get(`workFolder-${this.projectPath}`) || this.projectPath;
            }
            else {
                workFolder = storage_1.default.get('workFolder');
            }
            let directories = [];
            try {
                directories = await scanDirectory_1.default(workFolder);
            }
            catch (error) { }
            return {
                path: workFolder,
                directories,
            };
        }
        async setWorkFolderBySub({ args }) {
            const { subDirectory } = args;
            let workFolder = '';
            let newWorkFolder = '';
            let directories = [];
            let workFolderKey = 'workFolder';
            try {
                if (this.projectPath) {
                    workFolder = storage_1.default.get(`workFolder-${this.projectPath}`) || this.projectPath;
                    workFolderKey = `workFolder-${this.projectPath}`;
                }
                else {
                    workFolder = storage_1.default.get('workFolder');
                }
                newWorkFolder = path.join(workFolder, subDirectory);
                directories = await setWorkFolder(newWorkFolder, workFolderKey);
            }
            catch (e) {
                newWorkFolder = '/';
                directories = await setWorkFolder(newWorkFolder, workFolderKey);
            }
            return {
                path: newWorkFolder,
                directories,
            };
        }
        async setWorkFolder({ args }) {
            const { path } = args;
            let workFolderKey = 'workFolder';
            if (this.projectPath) {
                workFolderKey = `workFolder-${this.projectPath}`;
            }
            const directories = await setWorkFolder(path, workFolderKey);
            return {
                path,
                directories,
            };
        }
    };
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2V0dGluZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzI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