"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const utils_1 = require("../../core/libs/utils/utils");
const help_1 = __importDefault(require("../extends/command/help"));
const enum_1 = require("../utils/enum");
const env_1 = __importDefault(require("../utils/env"));
const preview_1 = __importDefault(require("../implements/preview"));
const fg_1 = require("../utils/fg");
class Preview extends help_1.default {
    async run() {
        const { args, flags } = this.parse(Preview);
        const preview = new preview_1.default();
        preview.eventTracker('preview_start', { tags: Object.assign(Object.assign({}, args), flags) });
        if (!args.path.includes('://')) {
            args.path = (0, utils_1.resolvePath)(args.path);
        }
        if (!flags.type) {
            flags.type = await preview.detectAppType(args.path);
        }
        const commonParams = {
            appId: flags.appId,
            path: args.path,
            platform: flags.platform,
        };
        if (flags.type === enum_1.TypeEnum.Gadget) {
            await preview.gadget(Object.assign(Object.assign({}, commonParams), { mode: flags.mode, watch: flags.watch, startPage: flags.startPage, debug: flags.debug, useNPM: flags.useNPM }));
        }
        else if (flags.type === enum_1.TypeEnum.H5) {
            this.error('This feature will be available soon');
        }
        else if (flags.type === enum_1.TypeEnum.Block) {
            const enableBlock = (0, fg_1.featureGatingBlock)();
            if (enableBlock) {
                await preview.block(Object.assign(Object.assign({}, commonParams), { host: 'workplace' }));
            }
            else {
                this.error('This feature will be available soon');
            }
        }
    }
}
exports.default = Preview;
Preview.aliases = ['p'];
Preview.description = 'Preview application on feishu client';
Preview.examples = [
    [
        '$ {{cli}} preview . -p pc -m sidebar',
        'preview PC Gadget in current folder with sidebar mode',
    ],
    [
        `$ {{cli}} preview . -p pc -m sidebar --startPage 'page/index/index?title="hello"'`,
        'preview PC Gadget in current folder with sidebar mode, and specify the startup page',
    ],
];
Preview.flags = {
    type: command_1.flags.enum({
        char: 't',
        description: 'project type',
        options: env_1.default.private
            ? [enum_1.TypeEnum.Gadget]
            : [enum_1.TypeEnum.Gadget, enum_1.TypeEnum.Block],
    }),
    platform: command_1.flags.enum({
        char: 'p',
        description: 'preview platform',
        required: false,
        options: enum_1.platform,
    }),
    mode: command_1.flags.enum({
        char: 'm',
        description: 'preview mode on PC',
        options: enum_1.mode,
    }),
    appId: command_1.flags.string({
        char: 'a',
        description: 'set appId',
        helpValue: 'cli_AppID',
    }),
    startPage: command_1.flags.string({
        char: 's',
        description: 'specify the startup page for application preview, gadget only',
    }),
    useNPM: command_1.flags.boolean({
        char: 'u',
        description: 'use npm',
        default: false,
    }),
    help: command_1.flags.help({
        char: 'h',
    }),
};
Preview.args = [
    {
        name: 'path',
        required: true,
    },
];
