"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "getCommands", {
    enumerable: true,
    get: function() {
        return getCommands;
    }
});
function _commander() {
    const data = require("commander");
    _commander = function() {
        return data;
    };
    return data;
}
function _promises() {
    const data = require("fs/promises");
    _promises = function() {
        return data;
    };
    return data;
}
function _path() {
    const data = require("path");
    _path = function() {
        return data;
    };
    return data;
}
function _config() {
    const data = require("~/compiler/config");
    _config = function() {
        return data;
    };
    return data;
}
function _fs() {
    const data = require("~/base/node/fs");
    _fs = function() {
        return data;
    };
    return data;
}
const _compiler = require("./compiler");
const _common = require("./common");
function _prompts() {
    const data = require("@inquirer/prompts");
    _prompts = function() {
        return data;
    };
    return data;
}
function _chalk() {
    const data = /*#__PURE__*/ _interop_require_default(require("chalk"));
    _chalk = function() {
        return data;
    };
    return data;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const EXAMPLES = [
    {
        id: "library",
        name: "library"
    },
    {
        id: "indicator",
        name: "indicator"
    }
];
const initProject = async ({ cwd })=>{
    cwd ??= process.cwd();
    await (0, _promises().mkdir)(cwd, {
        recursive: true
    });
    const configPath = (0, _path().resolve)(cwd, _config().QPC_CONFIG_FILENAME);
    let config;
    let firstInitialize = false;
    if (await (0, _fs().exists)(configPath)) {
        config = await (0, _compiler.loadQpcConfig)(configPath);
        console.log(`${_common.QPACE_BG_PREFIX}Project already exists at ${configPath}`);
        firstInitialize = false;
    } else {
        console.log(`${_common.QPACE_BG_PREFIX}Creating new project at ${configPath}`);
        config = (0, _config().getInitConfig)();
        firstInitialize = true;
    }
    let newConfig = {
        ...config
    };
    const usePython = await (0, _prompts().confirm)({
        message: "Do you want to use Python?",
        default: true
    });
    if (usePython) {
        const packageName = await (0, _prompts().input)({
            message: `
Enter Python package name.
${_chalk().default.yellowBright(`
import <PACKAGE_NAME> as pine
pine.my_indicator.custom_ma()
`)}
`,
            default: newConfig.python?.package ?? "qpace_artifact",
            required: false,
            validate: (x)=>{
                if (!(0, _compiler.isValidPipPackageName)(x)) {
                    return "Invalid Pip package name";
                }
                return true;
            }
        });
        newConfig.python = {
            ...newConfig.python,
            package: packageName
        };
        console.log();
    }
    const useJs = await (0, _prompts().confirm)({
        message: "Do you want to use JavaScript?",
        default: true
    });
    if (useJs) {
        const packageName = await (0, _prompts().input)({
            message: `
Enter JavaScript package name.
${_chalk().default.yellowBright(`
import * as pine from "<PACKAGE_NAME>"; 
pine.my_indicator.customMa()
`)}
`,
            default: (0, _config().tryGetJsPackageName)(newConfig.js) ?? "qpace_artifact",
            required: false,
            validate: (x)=>{
                if (!(0, _compiler.isValidNpmPackageName)(x)) {
                    return "Invalid NPM package name";
                }
                return true;
            }
        });
        newConfig.js = {
            ...newConfig.js,
            package: packageName
        };
        console.log();
    }
    const useExample = await (0, _prompts().select)({
        message: `Do you want to setup an example?`,
        default: true,
        choices: [
            {
                name: "yes",
                value: true
            },
            {
                name: "no",
                value: false
            }
        ]
    });
    if (useExample) {
        const exampleIds = await (0, _prompts().checkbox)({
            message: "Select examples",
            choices: EXAMPLES.map((r)=>({
                    value: r.id,
                    name: r.name
                }))
        });
        for (const exampleId of exampleIds){
            const exampleDir = (0, _path().resolve)(_common.EXAMPLES_DIR, exampleId);
            const srcFiles = await (0, _promises().readdir)(exampleDir).then((files)=>files.map((r)=>(0, _path().resolve)(exampleDir, r)));
            for (const srcFile of srcFiles){
                const srcPath = (0, _path().resolve)(exampleDir, srcFile);
                if (srcPath.endsWith(".py") && !usePython) {
                    continue;
                }
                if (srcPath.endsWith(".js") && !useJs) {
                    continue;
                }
                let destPath = (0, _path().resolve)(cwd, (0, _path().basename)(srcFile));
                if (await (0, _fs().exists)(destPath)) {
                    const overwrite = await (0, _prompts().confirm)({
                        message: `File ${destPath} already exists. Do you want to overwrite it?`,
                        default: true
                    });
                    if (!overwrite) {
                        destPath = (0, _path().resolve)(cwd, `${Date.now()}_${srcFile}`);
                    }
                }
                let content = await (0, _promises().readFile)(srcPath, "utf8");
                content = content.replaceAll("__QPC_PYTHON_PACKAGE__", newConfig.python?.package ?? "");
                content = content.replaceAll("__QPC_JS_PACKAGE__", (0, _config().tryGetJsPackageName)(newConfig.js) ?? "");
                await (0, _promises().writeFile)(destPath, content, "utf8");
                console.log(_chalk().default.cyanBright(`→ ${(0, _path().relative)(process.cwd(), destPath)}`));
            }
        }
        console.log();
    }
    await (0, _fs().writeJson)(configPath, newConfig, true);
    if (firstInitialize) {
        console.log(`${_common.QPACE_BG_PREFIX}Project initialized`);
    } else {
        console.log(`${_common.QPACE_BG_PREFIX}Project updated`);
    }
    console.log();
};
const getCommands = ()=>{
    return [
        new (_commander()).Command("init").description("Interactive project initialization").option("--cwd <path>", `Project root directory`).action(initProject)
    ];
};
