import { config, Helpers, notAllowedNames, } from 'tnp-core/lib';
import { _, crossPlatformPath, path } from 'tnp-core/lib';
import { HelpersTaon } from '../index';
export class LinkedProject {
    //#region static
    //#region static / from
    static from(options) {
        const linkedProj = _.merge(new LinkedProject(), _.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.git.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.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.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.exists([folderAbsPath, config.file.taon_jsonc]) &&
                !!Helpers.readJsonC([folderAbsPath, config.file.taon_jsonc])?.type &&
                Helpers.exists([folderAbsPath, config.file.package_json])) {
                return true;
            }
            return HelpersTaon.git.isGitRoot(folderAbsPath);
        })
            .map(folderAbsPath => {
            const relativePath = folderAbsPath.replace(insideLocation + '/', '');
            const projectName = path.basename(relativePath);
            const currentParentUrl = HelpersTaon.git.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.git.originHttpToSsh(url);
        }
        else if (setOrigin === 'http') {
            if (this.repoUrlHttp) {
                return this.repoUrlHttp;
            }
            url = HelpersTaon.git.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 (_.isString(linkedProjOrname)) {
                return LinkedProject.fromName(linkedProjOrname);
            }
            return LinkedProject.from(linkedProjOrname);
        });
        // .sort((a, b) => {
        //   return (a.relativeClonePath || '').localeCompare((b.relativeClonePath || ''));
        // });
        return _.merge(new LinkedPorjectsConfig(), _.cloneDeep(options));
    }
    //#endregion
    //#endregion
    prefix;
    skipRecrusivePush;
    resetOnlyChildren;
    projects;
}
//# sourceMappingURL=linked-project.js.map