//#region imports
import { config, Helpers__NS__error, Helpers__NS__exists, Helpers__NS__filesFrom, Helpers__NS__foldersFrom, Helpers__NS__mkdirp, Helpers__NS__removeFolderIfExists, Helpers__NS__removeIfExists, Helpers__NS__writeFile } from 'tnp-core/lib-prod';
import { path } from 'tnp-core/lib-prod';
import { HelpersTaon__NS__copy, HelpersTaon__NS__git__NS__checkout, HelpersTaon__NS__git__NS__clone, HelpersTaon__NS__git__NS__commit, HelpersTaon__NS__git__NS__pushCurrentBranch, HelpersTaon__NS__git__NS__stageAllFiles } from '../../index';
import { BaseFeatureForProject } from './base-feature-for-project';
//#endregion
/**
 * Easy way to create github pages
 * (or similar static site hosting)
 */
export class BaseStaticPages extends BaseFeatureForProject {
    TMP_STATIC_PAGE_SITE_REPOS = 'tmp-static-pages-sites-repos';
    //#region clear folder for gh-pages
    async cleanFolderForStaticPages(pathToBranchRepoFolder) {
        //#region @backendFunc
        const noAllowedToDeleteFiles = [config.file._gitignore];
        const noAllowedToDeleteFolders = ['.git'];
        for (const fileAbsPath of Helpers__NS__filesFrom(pathToBranchRepoFolder)) {
            if (!noAllowedToDeleteFiles.includes(path.basename(fileAbsPath))) {
                Helpers__NS__removeIfExists(fileAbsPath);
            }
        }
        for (const folderAbsPath of Helpers__NS__foldersFrom(pathToBranchRepoFolder)) {
            if (!noAllowedToDeleteFolders.includes(path.basename(folderAbsPath))) {
                Helpers__NS__removeFolderIfExists(folderAbsPath);
            }
        }
        //#endregion
    }
    //#endregion
    //#region main folder
    get mainFolder() {
        return this.project.cache['mainFolder'];
    }
    get mainFolderAbsPath() {
        return this.project.pathFor([this.TMP_STATIC_PAGE_SITE_REPOS, this.mainFolder]);
    }
    //#endregion
    //#region pages branch name
    get pagesBranchName() {
        return `${this.mainFolder}-pages`;
    }
    //#endregion
    //#region init
    async init(mainFolder, completeProcess = false) {
        //#region @backendFunc
        this.project.cache['mainFolder'] = mainFolder;
        const tempRepoPath = this.project.pathFor([
            this.TMP_STATIC_PAGE_SITE_REPOS,
            mainFolder,
        ]);
        if (!Helpers__NS__exists(tempRepoPath)) {
            await HelpersTaon__NS__git__NS__clone({
                cwd: this.project.pathFor(this.TMP_STATIC_PAGE_SITE_REPOS),
                url: this.project.git.originURL,
                destinationFolderName: mainFolder,
            });
        }
        HelpersTaon__NS__git__NS__checkout(tempRepoPath, this.pagesBranchName, {
            createBranchIfNotExists: true,
            switchBranchWhenExists: true,
        });
        await this.cleanFolderForStaticPages(tempRepoPath);
        const currentDocsFolderAbsPath = this.project.pathFor(config.folder.docs);
        if (Helpers__NS__filesFrom(currentDocsFolderAbsPath).length > 0 &&
            Helpers__NS__exists([currentDocsFolderAbsPath, 'index.html'])) {
            HelpersTaon__NS__copy(currentDocsFolderAbsPath, tempRepoPath, {
                recursive: true,
                overwrite: true,
            });
        }
        if (completeProcess) {
            HelpersTaon__NS__git__NS__stageAllFiles(tempRepoPath);
            HelpersTaon__NS__git__NS__commit(tempRepoPath, 'update docs');
            try {
                await HelpersTaon__NS__git__NS__pushCurrentBranch(tempRepoPath);
            }
            catch (error) {
                Helpers__NS__error(`Cannot push to ${this.pagesBranchName} branch`, false, true);
            }
            if (Helpers__NS__filesFrom(currentDocsFolderAbsPath).length > 0 &&
                Helpers__NS__exists([currentDocsFolderAbsPath, 'index.html'])) {
                Helpers__NS__removeFolderIfExists(currentDocsFolderAbsPath);
                Helpers__NS__mkdirp(currentDocsFolderAbsPath);
                Helpers__NS__writeFile([currentDocsFolderAbsPath, 'info.md'], `# ${this.project.name} docs`);
                this.project.git.addAndCommit(`chore: gh pages branch instead docs`);
                try {
                    await this.project.git.pushCurrentBranch({
                        forcePushNoQuestion: true,
                        force: true,
                    });
                }
                catch (error) {
                    Helpers__NS__error(`Cannot push to ${this.pagesBranchName} branch`, false, true);
                }
            }
        }
        //#endregion
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-static-pages.js.map