import { dotTaonFolder, Helpers__NS__exists, Helpers__NS__filesFrom, Helpers__NS__foldersFrom, Helpers__NS__info, Helpers__NS__isFolder, Helpers__NS__mkdirp, Helpers__NS__relative, Helpers__NS__remove, Helpers__NS__removeFileIfExists, Helpers__NS__run } from 'tnp-core/lib-prod';
import { crossPlatformPath, UtilsOs__NS__getRealHomeDir } from 'tnp-core/lib-prod';
import { HelpersTaon__NS__copyFile, HelpersTaon__NS__git__NS__getListOfCurrentGitChanges } from '../index';
/**
 * Class to save and restore temporary code
 * TODO fix removing files from repo
 */
export 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 crossPlatformPath([UtilsOs__NS__getRealHomeDir(), dotTaonFolder]);
        //#endregion
    }
    get tempPathRepo() {
        return crossPlatformPath([this.cwdRepo, 'ghtemp-code']);
    }
    init() {
        //#region @backendFunc
        if (!Helpers__NS__exists(this.cwdRepo)) {
            Helpers__NS__mkdirp(this.cwdRepo);
        }
        if (!Helpers__NS__exists(this.tempPathRepo)) {
            Helpers__NS__run(`git clone ${this.GHTEMP_CODE_REPO_PATH}`, {
                cwd: this.cwdRepo,
            }).sync();
        }
        Helpers__NS__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 Helpers__NS__filesFrom(this.tempPathRepo)) {
            Helpers__NS__removeFileIfExists(fileFromRepo);
        }
        for (const folderFromRepo of Helpers__NS__foldersFrom(this.tempPathRepo).filter(f => !f.endsWith('/.git'))) {
            Helpers__NS__remove(folderFromRepo, true);
        }
        const changesRelative = changes.map(f => Helpers__NS__relative(this.cwd, f));
        for (let index = 0; index < changes.length; index++) {
            const filesAbs = changes[index];
            if (!Helpers__NS__isFolder(filesAbs)) {
                HelpersTaon__NS__copyFile(filesAbs, crossPlatformPath([this.tempPathRepo, changesRelative[index]]));
            }
        }
        Helpers__NS__run(`git add . && git commit -m "changes" && git push origin master`, { cwd: this.tempPathRepo, output: true }).sync();
        Helpers__NS__info(`Saved ${changes.length} files in ${this.tempPathRepo}`);
        //#endregion
    }
    async restore() {
        //#region @backendFunc
        Helpers__NS__run(`git reset --soft HEAD~1`, { cwd: this.tempPathRepo }).sync();
        Helpers__NS__info(`Restored last commit in ${this.tempPathRepo}...`);
        const { created, modified } = HelpersTaon__NS__git__NS__getListOfCurrentGitChanges(this.tempPathRepo);
        const filesToRestore = [...created, ...modified].map(f => 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 (!Helpers__NS__isFolder(filesAbs)) {
                HelpersTaon__NS__copyFile(filesAbs, crossPlatformPath([this.cwd, filesToRestoreRelative[index]]));
            }
        }
        Helpers__NS__info(`Restored ${filesToRestore.length} files from ${this.tempPathRepo}`);
        return this;
        //#endregion
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/gh-temp-code.js.map