//#region imports
import { config } from 'tnp-core/lib-prod';
import { path, ___NS__cloneDeep, ___NS__first, ___NS__isFunction, ___NS__isObject } from 'tnp-core/lib-prod';
import { Helpers__NS__error, Helpers__NS__isClass } from 'tnp-core/lib-prod';
import { CLASS__NS__getMethodsNames, CLASS__NS__getName } from 'typescript-class-helpers/lib-prod';
import { HelpersTaon__NS__cliTool__NS__globalArgumentsParserTnp, HelpersTaon__NS__cliTool__NS__match } from '../../index';
import { BaseProject } from './base-project';
export class BaseStartConfig {
    //#region prepareArgs
    /**
     * @deprecated
     * use standard import / not default
     */
    static prepareArgs(cliClassArr) {
        const result = cliClassArr
            .map(c => Object.values(c))
            .reduce((a, b) => {
            return a.concat(b.map(funcOrClass => {
                return {
                    classOrFnName: CLASS__NS__getName(funcOrClass),
                    funcOrClass,
                };
            }));
        }, []).sort((a, b) => {
            if (a.classOrFnName < b.classOrFnName) {
                return -1;
            }
            if (a.classOrFnName > b.classOrFnName) {
                return 1;
            }
            return 0;
        });
        return result;
    }
    //#endregion
    //#region prepareFromFiles
    static prepareFromFiles(cliClassArr) {
        return this.prepareArgs(cliClassArr
            .map(c => require(path.resolve(c)).default)
            .filter(f => ___NS__isObject(f)));
    }
    //#endregion
    //#region fields
    argsv = process.argv;
    shortArgsReplaceConfig = {};
    functionsOrClasses = [];
    ProjectClass = BaseProject;
    callbackNotRecognizedCommand;
    /**
     * @deprecated
     */
    useStringArrForArgsFunctions;
    //#endregion
    //#region constructor
    constructor(options) {
        options = options ? options : {};
        for (const key in options) {
            if (Object.prototype.hasOwnProperty.call(options, key)) {
                const value = options[key];
                if (value) {
                    this[key] = value;
                }
            }
        }
        for (let index = 0; index < this.argsv.length; index++) {
            const arg = this.argsv[index];
            if (arg.endsWith('-debug-brk')) {
                this.argsv[index] = arg.replace(/\-debug\-brk$/, '');
            }
            if (arg.endsWith('-debug')) {
                this.argsv[index] = arg.replace(/\-debug$/, '');
            }
        }
        if (this.argsv?.length >= 2 &&
            (this.argsv[0].endsWith('bin/node') ||
                this.argsv[0].endsWith('bin\\node') ||
                this.argsv[0].endsWith('bin\\\\node') ||
                this.argsv[0].endsWith('\\\\node.exe') ||
                this.argsv[0].endsWith('\\node.exe'))) {
            this.argsv = this.argsv.slice(2);
        }
        const commandArgIndex = 0;
        const commandArg = this.argsv[commandArgIndex];
        if (commandArg) {
            const longerCommandVersion = this.shortArgsReplaceConfig[commandArg];
            if (longerCommandVersion) {
                this.argsv[commandArgIndex] = longerCommandVersion;
            }
        }
        let recognizedClassFnOrFunction = null;
        let restOfArgs = [];
        let methodNameToCall = undefined;
        let methodsOfRecognizedClass = [];
        let globalClassForGlobalCommands = null;
        for (const { classOrFnName, funcOrClass } of this.functionsOrClasses) {
            if (___NS__isFunction(funcOrClass)) {
                const classMethodsNames = CLASS__NS__getMethodsNames(funcOrClass).filter(f => !f.startsWith('_'));
                if (classOrFnName === '') {
                    globalClassForGlobalCommands = funcOrClass;
                }
                if (this.argsv.length === 0 && classOrFnName === '') {
                    recognizedClassFnOrFunction = funcOrClass;
                    restOfArgs = [];
                    methodNameToCall = '';
                    methodsOfRecognizedClass = classMethodsNames;
                    break;
                }
                const check = HelpersTaon__NS__cliTool__NS__match({
                    functionOrClassName: classOrFnName,
                    restOfArgs: ___NS__cloneDeep(this.argsv),
                    argsReplacements: this.shortArgsReplaceConfig,
                    classMethodsNames,
                });
                if (check.isMatch) {
                    recognizedClassFnOrFunction = funcOrClass;
                    restOfArgs = ___NS__cloneDeep(check.restOfArgs);
                    methodNameToCall = check.methodNameToCall;
                    methodsOfRecognizedClass = classMethodsNames;
                    // console.log('--- recognized command ---', { classOrFnName, classMethodsNames })
                }
            }
        }
        if (recognizedClassFnOrFunction) {
            global?.spinner?.stop();
            // console.log('--- recognized command ---', {
            //   recognizedClassFnOrFunction,
            //   methodNameToCall,
            //   restOfArgs,
            //   methodsOfRecognizedClass,
            // });
            if (Helpers__NS__isClass(recognizedClassFnOrFunction)) {
                // console.log('USING FROM CLASS')
                const obj = new recognizedClassFnOrFunction(HelpersTaon__NS__cliTool__NS__globalArgumentsParserTnp(restOfArgs, this.ProjectClass), methodNameToCall, this.ProjectClass.ins.nearestTo(process.cwd()), process.cwd(), this.ProjectClass.ins);
            }
            else {
                // console.log('USING FROM FUNCTION')
                if (this.useStringArrForArgsFunctions) {
                    recognizedClassFnOrFunction.apply({}, [
                        HelpersTaon__NS__cliTool__NS__globalArgumentsParserTnp(restOfArgs, this.ProjectClass)
                            .split(' '),
                    ]);
                }
                else {
                    recognizedClassFnOrFunction.apply({}, [
                        HelpersTaon__NS__cliTool__NS__globalArgumentsParserTnp(restOfArgs, this.ProjectClass),
                    ]);
                }
            }
            process.stdin.resume();
        }
        else {
            if (___NS__isFunction(this.callbackNotRecognizedCommand)) {
                // console.log(`this.argsv `, this.argsv)
                this.callbackNotRecognizedCommand({
                    firstArg: ___NS__first(this.argsv),
                    runGlobalCommandByName: (commandName) => {
                        if (globalClassForGlobalCommands) {
                            const obj = new globalClassForGlobalCommands(HelpersTaon__NS__cliTool__NS__globalArgumentsParserTnp(this.argsv, this.ProjectClass), commandName, this.ProjectClass.ins.nearestTo(process.cwd()), process.cwd(), this.ProjectClass.ins);
                        }
                        else {
                            Helpers__NS__error(`Global class for global commands not found`, false, true);
                        }
                    },
                });
            }
            else {
                Helpers__NS__error(`[${config.frameworkName}] Command not recognized`, false, true);
            }
        }
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-start-config.backend.js.map