import { config, notAllowedNames, Helpers__NS__exists, Helpers__NS__foldersFrom, Helpers__NS__logWarn, Helpers__NS__readJsonC } from 'tnp-core/lib-prod';
import { crossPlatformPath, path, ___NS__cloneDeep, ___NS__isString, ___NS__merge } from 'tnp-core/lib-prod';
import { HelpersTaon__NS__git__NS__getOriginURL, HelpersTaon__NS__git__NS__isGitRoot, HelpersTaon__NS__git__NS__isValidRepoUrl, HelpersTaon__NS__git__NS__originHttpToSsh, HelpersTaon__NS__git__NS__originSshToHttp } from '../index';
export class LinkedProject {
    //#region static
    //#region static / from
    static from(options) {
        const linkedProj = ___NS__merge(new LinkedProject(), ___NS__cloneDeep(options));
        if (!linkedProj.relativeClonePath) {
            linkedProj.relativeClonePath = path.basename(linkedProj.remoteUrl().replace('.git', ''));
        }
        if (!linkedProj.defaultBranch) {
            linkedProj.defaultBranch = 'master';
        }
        return linkedProj;
    }
    //#endregion
    //#region static / from name
    static fromName(projectName, currentRemoteUrl, currentBranch) {
        //#region @backendFunc
        const isProperCurrentRemoteUrl = currentRemoteUrl && HelpersTaon__NS__git__NS__isValidRepoUrl(currentRemoteUrl);
        const repoUrl = isProperCurrentRemoteUrl
            ? currentRemoteUrl.replace(path.basename(currentRemoteUrl), `${projectName}.git`)
            : void 0;
        return LinkedProject.from({
            repoUrl,
            defaultBranch: currentBranch,
            relativeClonePath: projectName,
        });
        //#endregion
    }
    //#endregion
    //#region static / detect
    static detect(insideLocation, options) {
        //#region @backendFunc
        options = options || {};
        const { recursive, checkAlsoNonRepos } = options;
        insideLocation = crossPlatformPath(insideLocation).replace(/\/$/, '');
        const detectedLinkedProjects = Helpers__NS__foldersFrom(insideLocation, {
            recursive,
            omitRootFolders: [
                'src',
                'environments',
                'docs',
                'node_modules',
                'dist',
                'bin',
                'old',
                'local_release',
                'projects',
                'browser',
                'websql',
            ],
            omitRootFoldersThatStartWith: ['tmp-', '.', 'dist-'],
        })
            .filter(folderAbsPath => {
            if (notAllowedNames.includes(path.basename(folderAbsPath))) {
                Helpers__NS__logWarn(`[${config.frameworkName}-helpers][linked-projects] ` +
                    `Skipping folder ${folderAbsPath} because it has not allowed name`);
                return false;
            }
            // console.log('folderAbsPath', folderAbsPath);
            // Helpers.checkIfNameAllowedForTaonProj(path.basename(folderAbsPath)) &&
            if (checkAlsoNonRepos &&
                Helpers__NS__exists([folderAbsPath, config.file.taon_jsonc]) &&
                !!Helpers__NS__readJsonC([folderAbsPath, config.file.taon_jsonc])?.type &&
                Helpers__NS__exists([folderAbsPath, config.file.package_json])) {
                return true;
            }
            return HelpersTaon__NS__git__NS__isGitRoot(folderAbsPath);
        })
            .map(folderAbsPath => {
            const relativePath = folderAbsPath.replace(insideLocation + '/', '');
            const projectName = path.basename(relativePath);
            const currentParentUrl = HelpersTaon__NS__git__NS__getOriginURL(folderAbsPath);
            return LinkedProject.from({
                repoUrl: currentParentUrl ? currentParentUrl : void 0,
                relativeClonePath: projectName,
            });
        });
        // .sort((a, b) => {
        //   return (a.relativeClonePath || '').localeCompare((b.relativeClonePath || ''));
        // });
        return detectedLinkedProjects;
        //#endregion
    }
    //#endregion
    //#endregion
    /**
     * sometime there is project that has one project
     * embede inside another project
     */
    internalRealtiveProjectPath;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrlSsh;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrlHttp;
    /**
     * don't access directly, use remoteUrl() instead
     */
    repoUrl;
    /**
     * url for git repo
     */
    remoteUrl() {
        if (this.repoUrl) {
            return this.repoUrl;
        }
        if (this.repoUrlHttp) {
            return this.repoUrlHttp;
        }
        return this.repoUrlSsh;
    }
    remoteUrlTransformed(setOrigin) {
        //#region @backendFunc
        let url = this.remoteUrl();
        if (setOrigin === 'ssh') {
            if (this.repoUrlSsh) {
                return this.repoUrlSsh;
            }
            url = HelpersTaon__NS__git__NS__originHttpToSsh(url);
        }
        else if (setOrigin === 'http') {
            if (this.repoUrlHttp) {
                return this.repoUrlHttp;
            }
            url = HelpersTaon__NS__git__NS__originSshToHttp(url);
        }
        return url;
        //#endregion
    }
    defaultBranch;
    purpose;
    relativeClonePath;
}
export class LinkedPorjectsConfig {
    //#region static
    //#region static / from
    static from(options) {
        options = options || {};
        options.projects = (options.projects || []).map(linkedProjOrname => {
            if (___NS__isString(linkedProjOrname)) {
                return LinkedProject.fromName(linkedProjOrname);
            }
            return LinkedProject.from(linkedProjOrname);
        });
        // .sort((a, b) => {
        //   return (a.relativeClonePath || '').localeCompare((b.relativeClonePath || ''));
        // });
        return ___NS__merge(new LinkedPorjectsConfig(), ___NS__cloneDeep(options));
    }
    //#endregion
    //#endregion
    prefix;
    skipRecrusivePush;
    resetOnlyChildren;
    projects;
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/linked-project.js.map