"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.create = void 0;
var lodash_1 = require("lodash");
var inquirer_1 = __importDefault(require("inquirer"));
var path_1 = require("path");
var zeppos_app_utils_1 = require("zeppos-app-utils");
var AppType = zeppos_app_utils_1.config.AppType, getDeviceConf = zeppos_app_utils_1.config.getDeviceConf;
var inquirerFunc = function (args, _a) {
    var appTemplateChoices = _a.appTemplateChoices, workoutExtTemplateChoices = _a.workoutExtTemplateChoices, watchfaceTemplateChoices = _a.watchfaceTemplateChoices;
    return __awaiter(void 0, void 0, void 0, function () {
        var _b, promptAppType, _c, promptOsVersion, _d, promptTemplate, _e, promptWithAppSide, _f, promptWithSettings, _g, promptBuildTargets, _h, configVersion, _j, deviceTargets, OSV2DeviceTargetNames;
        return __generator(this, function (_k) {
            switch (_k.label) {
                case 0:
                    _b = args.appType, promptAppType = _b === void 0 ? '' : _b, _c = args.OSVersion, promptOsVersion = _c === void 0 ? '' : _c, _d = args.template, promptTemplate = _d === void 0 ? '' : _d, _e = args.withAppSide, promptWithAppSide = _e === void 0 ? false : _e, _f = args.withSettings, promptWithSettings = _f === void 0 ? false : _f, _g = args.buildTargets, promptBuildTargets = _g === void 0 ? [] : _g, _h = args.configVersion, configVersion = _h === void 0 ? 'v2' : _h;
                    _j = getDeviceConf(), deviceTargets = _j.deviceTargets, OSV2DeviceTargetNames = _j.OSV2DeviceTargetNames;
                    return [4, inquirer_1["default"].prompt([
                            {
                                name: 'appType',
                                type: 'list',
                                when: function (args) {
                                    var bool = !promptAppType || !Object.values(AppType).includes(promptAppType);
                                    if (!bool) {
                                        args.appType = promptAppType.toUpperCase();
                                    }
                                    return bool;
                                },
                                message: 'What type of application do you want to create?',
                                choices: Object.keys(AppType)
                            },
                            {
                                name: 'OSVersion',
                                type: 'list',
                                when: function (args) {
                                    var isWorkoutExtension = args.appType.toLowerCase() === AppType.WORKOUT_EXTENSION;
                                    var bool = !promptOsVersion || !['v1', 'v2'].includes(promptOsVersion);
                                    if (!bool) {
                                        args.OSVersion = promptOsVersion;
                                    }
                                    if (isWorkoutExtension)
                                        args.OSVersion = 'v3.5';
                                    return bool && !isWorkoutExtension;
                                },
                                message: 'Which OS version API to develop with?',
                                choices: function (args) {
                                    var isWatchFace = args.appType.toLowerCase() === AppType.WATCHFACE;
                                    var options = [{
                                            name: 'OS 1.0 API (Build products can run on devices with OS 1.0 and above)',
                                            value: 'v1'
                                        },
                                        {
                                            name: 'OS 2.0 API (Build products can run on devices with OS 2.0 and above)',
                                            value: 'v2'
                                        },
                                        {
                                            name: 'OS 3.0 API (Build products can run on devices with OS 3.0 and above)',
                                            value: 'v3'
                                        }, {
                                            name: 'OS 4.0 API (Build products can run on devices with OS 4.0 and above)',
                                            value: 'v4'
                                        }];
                                    if (isWatchFace) {
                                        options = options.slice(0, 2);
                                    }
                                    return __spreadArray([
                                        new inquirer_1["default"].Separator(),
                                        new inquirer_1["default"].Separator('Device Basic Information: https://docs.zepp.com/docs/reference/related-resources/device-list/'),
                                        new inquirer_1["default"].Separator()
                                    ], options, true);
                                }
                            },
                            {
                                name: 'template',
                                type: 'list',
                                when: function (args) {
                                    var isApp = args.appType.toLowerCase() === AppType.APP;
                                    var objMap = appTemplateChoices[args.OSVersion].find(function (val) {
                                        var name = val.name === 'Hello World' ? 'Hello_World' : val.name;
                                        return name === promptTemplate;
                                    });
                                    if (isApp && objMap) {
                                        args.template = objMap.value;
                                    }
                                    return isApp && !objMap;
                                },
                                message: 'Which app template do you want to use?',
                                choices: function (args) {
                                    var val = appTemplateChoices[args.OSVersion] || [];
                                    return __spreadArray([
                                        new inquirer_1["default"].Separator(),
                                        new inquirer_1["default"].Separator('Choose a app template')
                                    ], val, true);
                                }
                            },
                            {
                                name: 'template',
                                type: 'list',
                                when: function (args) {
                                    var isWorkoutExtension = args.appType.toLowerCase() === AppType.WORKOUT_EXTENSION;
                                    if (!isWorkoutExtension)
                                        return isWorkoutExtension;
                                    var objMap = workoutExtTemplateChoices[args.OSVersion].find(function (val) {
                                        var name = val.name === 'Hello World' ? 'Hello_World' : val.name;
                                        return name === promptTemplate;
                                    });
                                    if (isWorkoutExtension && objMap) {
                                        args.template = objMap.value;
                                    }
                                    return isWorkoutExtension && !objMap;
                                },
                                message: 'Which app template do you want to use?',
                                choices: function (args) {
                                    var val = workoutExtTemplateChoices[args.OSVersion] || [];
                                    return __spreadArray([
                                        new inquirer_1["default"].Separator(),
                                        new inquirer_1["default"].Separator('Choose a app template')
                                    ], val, true);
                                }
                            },
                            {
                                name: 'template',
                                type: 'list',
                                when: function (args) {
                                    var isWatchFace = args.appType.toLowerCase() === AppType.WATCHFACE;
                                    var objMap = watchfaceTemplateChoices[args.OSVersion].find(function (val) { return val.name === promptTemplate; });
                                    if (isWatchFace && objMap) {
                                        args.template = objMap.value;
                                    }
                                    return isWatchFace && !objMap;
                                },
                                message: 'Which watchface template do you want to use?',
                                choices: function (args) {
                                    var val = watchfaceTemplateChoices[args.OSVersion] || [];
                                    return __spreadArray([
                                        new inquirer_1["default"].Separator(),
                                        new inquirer_1["default"].Separator('Choose a watchface template')
                                    ], val, true);
                                }
                            },
                            {
                                name: 'withAppSide',
                                type: 'confirm',
                                when: function (args) {
                                    var isApp = args.appType.toLowerCase() === AppType.APP;
                                    var isEmpty = args.template === 0;
                                    if (isApp && isEmpty && promptWithAppSide) {
                                        args.withAppSide = promptWithAppSide === 'no' ? false : true;
                                        return false;
                                    }
                                    return isApp && isEmpty;
                                },
                                message: 'Should this application contain a app-side component?'
                            },
                            {
                                name: 'withSettings',
                                type: 'confirm',
                                when: function (args) {
                                    var withAppSide = args.withAppSide;
                                    if (withAppSide && promptWithSettings) {
                                        args.withSettings = promptWithSettings === 'no' ? false : true;
                                        return false;
                                    }
                                    return withAppSide;
                                },
                                message: 'Should this application contain a settings component?'
                            }, {
                                name: 'shapeOfPlatform',
                                type: 'checkbox',
                                when: function (args) {
                                    var isApp = args.appType.toLowerCase() === AppType.APP;
                                    var isEmpty = args.template === 0;
                                    var osVersionAboveV2 = args.OSVersion.replace('v', '') - 2 > 0;
                                    return osVersionAboveV2 && isApp && isEmpty;
                                },
                                choices: [
                                    new inquirer_1["default"].Separator(),
                                    new inquirer_1["default"].Separator('Choose the shape of the device'),
                                    { name: 'Round-screen smartwatch device', value: 'r', checked: true },
                                    { name: 'Square-screen smartwatch device', value: 's' },
                                    { name: 'Band device', value: 'b' },
                                ],
                                "default": 'r',
                                message: 'On which device platform do you plan to install the application?'
                            }
                        ]).then(function (args) { return __awaiter(void 0, void 0, void 0, function () {
                            var choicePlatforms_1, answer;
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        if (!(typeof promptBuildTargets === 'string')) return [3, 1];
                                        args.buildTargets = (0, lodash_1.intersection)(Object.keys(deviceTargets), promptBuildTargets.split(','));
                                        return [3, 3];
                                    case 1:
                                        if (!(configVersion !== 'v3')) return [3, 3];
                                        choicePlatforms_1 = [];
                                        switch (args.OSVersion) {
                                            case 'v1':
                                                choicePlatforms_1 = Object.keys(deviceTargets);
                                                break;
                                            case 'v2':
                                                choicePlatforms_1 = OSV2DeviceTargetNames;
                                                break;
                                            default:
                                                break;
                                        }
                                        if (!choicePlatforms_1.length) return [3, 3];
                                        return [4, inquirer_1["default"].prompt({
                                                name: 'buildTargets',
                                                type: 'checkbox',
                                                when: function () { return args.template === 0; },
                                                choices: choicePlatforms_1.map(function (platform) { return ({
                                                    name: deviceTargets[platform].deviceName,
                                                    value: platform,
                                                    checked: platform === choicePlatforms_1[0]
                                                }); }),
                                                "default": choicePlatforms_1[0],
                                                message: 'Which platforms of this application should be built?'
                                            })];
                                    case 2:
                                        answer = _a.sent();
                                        Object.assign(args, answer);
                                        _a.label = 3;
                                    case 3: return [2, args];
                                }
                            });
                        }); })];
                case 1: return [2, _k.sent()];
            }
        });
    });
};
var create = function (args) { return __awaiter(void 0, void 0, void 0, function () {
    var cmd, _a, projectName, beforeFiles, cwdPath;
    return __generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                cmd = args._[0], _a = args.projectName, projectName = _a === void 0 ? '' : _a;
                beforeFiles = [];
                cwdPath = (0, path_1.resolve)('./');
                return [4, (0, zeppos_app_utils_1.checkCreateName)(cmd, cwdPath, projectName, 'CLI').then(function (res) {
                        beforeFiles = res.beforeFiles;
                    })["catch"](function (res) {
                        zeppos_app_utils_1.logger.error(res.message);
                        process.exit(1);
                    })];
            case 1:
                _b.sent();
                if (cmd === 'create') {
                    cwdPath = (0, path_1.resolve)(cwdPath, projectName);
                }
                return [4, zeppos_app_utils_1.modules.createX(cwdPath, args, inquirerFunc, beforeFiles).then(function () { })];
            case 2: return [2, _b.sent()];
        }
    });
}); };
exports.create = create;
