"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GhTempCode = void 0;
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const index_1 = require("../index");
/**
 * Class to save and restore temporary code
 * TODO fix removing files from repo
 */
class GhTempCode {
    cwd;
    project;
    constructor(cwd, project) {
        this.cwd = cwd;
        this.project = project;
    }
    // TODO save it in db
    GHTEMP_CODE_REPO_PATH = 'git@github.com:darekf77/ghtemp-code.git';
    get cwdRepo() {
        //#region @backendFunc
        return (0, lib_2.crossPlatformPath)([lib_2.UtilsOs.getRealHomeDir(), lib_1.dotTaonFolder]);
        //#endregion
    }
    get tempPathRepo() {
        return (0, lib_2.crossPlatformPath)([this.cwdRepo, 'ghtemp-code']);
    }
    init() {
        //#region @backendFunc
        if (!lib_1.Helpers.exists(this.cwdRepo)) {
            lib_1.Helpers.mkdirp(this.cwdRepo);
        }
        if (!lib_1.Helpers.exists(this.tempPathRepo)) {
            lib_1.Helpers.run(`git clone ${this.GHTEMP_CODE_REPO_PATH}`, {
                cwd: this.cwdRepo,
            }).sync();
        }
        lib_1.Helpers.run(`git reset --hard && git pull origin master`, {
            cwd: this.tempPathRepo,
        }).sync();
        return this;
        //#endregion
    }
    async save() {
        //#region @backendFunc
        const changes = [
            ...this.project.git.listOfCurrentGitChanges.created,
            ...this.project.git.listOfCurrentGitChanges.modified,
        ];
        for (const fileFromRepo of lib_1.Helpers.filesFrom(this.tempPathRepo)) {
            lib_1.Helpers.removeFileIfExists(fileFromRepo);
        }
        for (const folderFromRepo of lib_1.Helpers.foldersFrom(this.tempPathRepo).filter(f => !f.endsWith('/.git'))) {
            lib_1.Helpers.remove(folderFromRepo, true);
        }
        const changesRelative = changes.map(f => lib_1.Helpers.relative(this.cwd, f));
        for (let index = 0; index < changes.length; index++) {
            const filesAbs = changes[index];
            if (!lib_1.Helpers.isFolder(filesAbs)) {
                index_1.HelpersTaon.copyFile(filesAbs, (0, lib_2.crossPlatformPath)([this.tempPathRepo, changesRelative[index]]));
            }
        }
        lib_1.Helpers.run(`git add . && git commit -m "changes" && git push origin master`, { cwd: this.tempPathRepo, output: true }).sync();
        lib_1.Helpers.info(`Saved ${changes.length} files in ${this.tempPathRepo}`);
        //#endregion
    }
    async restore() {
        //#region @backendFunc
        lib_1.Helpers.run(`git reset --soft HEAD~1`, { cwd: this.tempPathRepo }).sync();
        lib_1.Helpers.info(`Restored last commit in ${this.tempPathRepo}...`);
        const { created, modified } = index_1.HelpersTaon.git.getListOfCurrentGitChanges(this.tempPathRepo);
        const filesToRestore = [...created, ...modified].map(f => (0, lib_2.crossPlatformPath)([this.tempPathRepo, f]));
        const filesToRestoreRelative = filesToRestore.map(f => f.replace(this.tempPathRepo + '/', ''));
        for (let index = 0; index < filesToRestore.length; index++) {
            const filesAbs = filesToRestore[index];
            if (!lib_1.Helpers.isFolder(filesAbs)) {
                index_1.HelpersTaon.copyFile(filesAbs, (0, lib_2.crossPlatformPath)([this.cwd, filesToRestoreRelative[index]]));
            }
        }
        lib_1.Helpers.info(`Restored ${filesToRestore.length} files from ${this.tempPathRepo}`);
        return this;
        //#endregion
    }
}
exports.GhTempCode = GhTempCode;
//# sourceMappingURL=gh-temp-code.js.map