"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("../../core");
const types_1 = require("../../core/libs/modules/creator/generator/types");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const base_1 = __importDefault(require("./base"));
const update_1 = __importDefault(require("../utils/update"));
const lang_1 = require("../utils/lang");
function isTemplateInFg(fgKey) {
    var _a;
    if (!fgKey)
        return true;
    const data = update_1.default.getFGData();
    const features = (_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.features;
    return features.includes(fgKey);
}
class Creator extends base_1.default {
    constructor() {
        super([core_1.ModuleName.creator, core_1.ModuleName.static, core_1.ModuleName.ui]);
        this.appItems = [];
    }
    static getAvaliableAppItems() {
        const { list = [] } = update_1.default.getUpdateData().cli.templateMeta || {};
        return list.filter((item) => isTemplateInFg(item.fg));
    }
    enterProjectPath() {
        return this.manager.execute(core_1.ModuleName.ui, types_1.UICmdEnum.input, {
            placeholder: 'Please enter project path',
            default: 'my-demo',
            checker: this.checkProjectPath,
        });
    }
    async checkProjectPath(projectPath) {
        const absoluteProjectPath = path_1.default.resolve(projectPath);
        try {
            await fs_extra_1.default.ensureDir(absoluteProjectPath);
        }
        catch (e) {
            return true;
        }
        const hasFile = !!(await fs_extra_1.default.readdir(absoluteProjectPath)).length;
        if (hasFile) {
            throw new Error(`${absoluteProjectPath} is not empty`);
        }
        return true;
    }
    async chooseAppType(appType) {
        const appItems = Creator.getAvaliableAppItems();
        if (!appItems.length) {
            throw new Error('no avaliable template');
        }
        if (appType) {
            const result = appItems.find((item) => item.type === appType);
            if (result) {
                return result;
            }
            else {
                throw new Error(`appType ${appType} is not valid`);
            }
        }
        else {
            if (appItems.length > 1) {
                const lang = (0, lang_1.getLang)();
                const typeIndex = await this.manager.execute(core_1.ModuleName.ui, types_1.UICmdEnum.pick, appItems.map((t, index) => {
                    return {
                        name: t.type +
                            (t.desc[lang] && lang === 'zh-CN' ? `(${t.desc[lang]})` : ''),
                        value: index,
                    };
                }), 'Please select app type');
                return appItems[typeIndex];
            }
            else {
                return appItems[0];
            }
        }
    }
    async chooseSubtype(options) {
        var _a;
        const subtypes = (((_a = options.appType) === null || _a === void 0 ? void 0 : _a.subtypes) || []).filter((item) => isTemplateInFg(item.fg));
        if (!subtypes.length) {
            return undefined;
        }
        if (options.subtype) {
            const subtypeItem = subtypes.find((subtype) => subtype.type === options.subtype);
            if (subtypeItem) {
                return subtypeItem;
            }
            else {
                throw new Error(`subtype ${options.subtype} is not valid`);
            }
        }
        else {
            if (subtypes.length > 1) {
                const lang = (0, lang_1.getLang)();
                const subtypeIndex = await this.manager.execute(core_1.ModuleName.ui, types_1.UICmdEnum.pick, subtypes.map((meta, index) => {
                    return {
                        name: meta.type +
                            (meta.desc[lang] && lang === 'zh-CN'
                                ? `(${meta.desc[lang]})`
                                : ''),
                        value: index,
                    };
                }), 'Please select subtype');
                const subtypeItem = subtypes[subtypeIndex];
                if (!subtypeItem) {
                    throw new Error(`subtype ${options.subtype} is not valid`);
                }
                return subtypeItem;
            }
            else {
                return subtypes[0];
            }
        }
    }
    async chooseTemplate(options) {
        const lang = (0, lang_1.getLang)();
        const templates = options.templateCate.templates.filter((item) => isTemplateInFg(item.fg));
        if (!templates.length) {
            throw new Error('no avaliable template');
        }
        if (options.templateName) {
            const templateItem = templates.find((t) => t.name === options.templateName);
            if (templateItem) {
                return templateItem;
            }
            else {
                throw new Error(`template ${options.templateName} is not valid`);
            }
        }
        else {
            if (templates.length > 1) {
                const templateIndex = await this.manager.execute(core_1.ModuleName.ui, types_1.UICmdEnum.pick, templates.map((meta, index) => {
                    return {
                        name: meta.name + (meta.desc[lang] ? `(${meta.desc[lang]})` : ''),
                        value: index,
                    };
                }), 'please select template');
                const templateItem = templates[templateIndex];
                if (!templateItem) {
                    throw new Error(`template ${options.templateName} name is not valid`);
                }
                return templateItem;
            }
            else {
                return templates[0];
            }
        }
    }
    async generate(options) {
        await this.manager.execute(core_1.ModuleName.creator, core_1.CreatorCmdEnum.generateV2, {
            templateCate: options.templateCate,
            projectPath: options.projectPath,
            templateConfig: options.templateConfig,
        });
    }
}
exports.default = Creator;
