//#region imports
import { fileName, folderName } from 'tnp-core/lib';
import { Helpers, crossPlatformPath, fse } from 'tnp-core/lib';
import { HelpersTaon } from '../../index';
import { BaseFeatureForProject } from './base-feature-for-project';
//#endregion
export class BaseFileFoldersOperations extends 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 [
            folderName._bin,
            folderName.src,
            folderName.docs,
            fileName.package_json,
            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 {
                fse.unlinkSync(proj.pathFor(folderName.node_modules));
            }
            catch (error) { }
            Helpers.removeSymlinks(proj.location);
            Helpers.removeSymlinks(proj.pathFor(folderName.node_modules));
            if (!options.skipChildren) {
                for (const child of this.project.children) {
                    removeProj(child);
                }
            }
        };
        removeProj(this.project);
        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 = crossPlatformPath([
                    rootDest,
                    proj.basename,
                    relativePath,
                ]);
                if (Helpers.exists(sourcePath)) {
                    if (Helpers.isFolder(sourcePath)) {
                        console.log(`Copying folder ${sourcePath} to ${destPath}`);
                        HelpersTaon.copy(sourcePath, destPath);
                    }
                    else {
                        console.log(`Copying file ${sourcePath} to ${destPath}`);
                        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, 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
    }
}
//# sourceMappingURL=base-file-folders-operations.js.map