"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseFileFoldersOperations = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const index_1 = require("../../index");
const base_feature_for_project_1 = require("./base-feature-for-project");
//#endregion
class BaseFileFoldersOperations extends base_feature_for_project_1.BaseFeatureForProject {
    //#region files and folders to copy when copying project
    /**
     * Relative to project root
     * @returns list of files and folders to copy when copying project
     */
    fielsAndFoldersToCopy() {
        return [
            lib_1.folderName._bin,
            lib_1.folderName.src,
            lib_1.folderName.docs,
            lib_1.fileName.package_json,
            lib_1.fileName.README_MD,
        ];
    }
    //#endregion
    //#region move project to
    async moveProjectTo(destination) {
        await this.copyProjectTo(destination);
        this.deleteProject();
    }
    //#endregion
    //#region delete project
    deleteProject(options) {
        //#region @backendFunc
        options = options || {};
        const removeProj = (proj) => {
            try {
                lib_2.fse.unlinkSync(proj.pathFor(lib_1.folderName.node_modules));
            }
            catch (error) { }
            lib_2.Helpers.removeSymlinks(proj.location);
            lib_2.Helpers.removeSymlinks(proj.pathFor(lib_1.folderName.node_modules));
            if (!options.skipChildren) {
                for (const child of this.project.children) {
                    removeProj(child);
                }
            }
        };
        removeProj(this.project);
        lib_2.Helpers.remove(this.project.location);
        // this.ins.remove(this.project);
        //#endregion
    }
    //#endregion
    //#region copy project to
    async copyProjectTo(destination, options) {
        //#region @backendFunc
        options = options || {};
        const fielsAndFoldersToCopy = this.fielsAndFoldersToCopy();
        const copyProj = async (proj, rootDest) => {
            for (const relativePath of fielsAndFoldersToCopy) {
                const sourcePath = proj.pathFor(relativePath);
                const destPath = (0, lib_2.crossPlatformPath)([
                    rootDest,
                    proj.basename,
                    relativePath,
                ]);
                if (lib_2.Helpers.exists(sourcePath)) {
                    if (lib_2.Helpers.isFolder(sourcePath)) {
                        console.log(`Copying folder ${sourcePath} to ${destPath}`);
                        index_1.HelpersTaon.copy(sourcePath, destPath);
                    }
                    else {
                        console.log(`Copying file ${sourcePath} to ${destPath}`);
                        index_1.HelpersTaon.copyFile(sourcePath, destPath);
                    }
                }
                else {
                    console.warn(`Path ${sourcePath} does not exist. Skipping...`);
                }
            }
            // const copiedProject = this.project.ins.From([rootDest, proj.basename]);
            // if (!copiedProject) {
            //   throw new Error(
            //     `Project was not copied correctly. Cannot find it in ${crossPlatformPath([rootDest, proj.basename])}`,
            //   );
            // }
            // try {
            //   await copiedProject.init();
            // } catch (error) {}
            if (!options.skipChildren) {
                const children = proj.children.filter(f => f.location !== proj.location); // prevent self
                for (const child of children) {
                    await copyProj(child, (0, lib_2.crossPlatformPath)([rootDest, proj.basename]));
                    // this.ins.add(
                    //   this.ins.From([destination, path.basename(child.location)]),
                    // );
                }
                // try {
                //   await copiedProject.refreshChildrenProjects();
                // } catch (error) {}
            }
        };
        await copyProj(this.project, destination);
        // this.ins.add(this.ins.From(destination));
        //#endregion
    }
}
exports.BaseFileFoldersOperations = BaseFileFoldersOperations;
//# sourceMappingURL=base-file-folders-operations.js.map