"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbedCommand = void 0;
const yok_1 = require("../../common/yok");
const prepare_1 = require("../prepare");
const path_1 = require("path");
const color_1 = require("../../color");
class EmbedCommand extends prepare_1.PrepareCommand {
    constructor($options, $prepareController, $platformValidationService, $projectData, $platformCommandParameter, $platformsDataService, $prepareDataService, $migrateController, $logger, $fs, $projectConfigService) {
        super($options, $prepareController, $platformValidationService, $projectData, $platformCommandParameter, $platformsDataService, $prepareDataService, $migrateController);
        this.$options = $options;
        this.$prepareController = $prepareController;
        this.$platformValidationService = $platformValidationService;
        this.$projectData = $projectData;
        this.$platformCommandParameter = $platformCommandParameter;
        this.$platformsDataService = $platformsDataService;
        this.$prepareDataService = $prepareDataService;
        this.$migrateController = $migrateController;
        this.$logger = $logger;
        this.$fs = $fs;
        this.$projectConfigService = $projectConfigService;
    }
    resolveHostProjectPath(hostProjectPath) {
        if (hostProjectPath.charAt(0) === ".") {
            const projectDir = this.$projectData.projectDir;
            return (0, path_1.resolve)(projectDir, hostProjectPath);
        }
        return (0, path_1.resolve)(hostProjectPath);
    }
    async execute(args) {
        const hostProjectPath = args[1];
        const resolvedHostProjectPath = this.resolveHostProjectPath(hostProjectPath);
        if (!this.$fs.exists(resolvedHostProjectPath)) {
            this.$logger.error(`The host project path ${color_1.color.yellow(hostProjectPath)} (resolved to: ${color_1.color.yellow.dim(resolvedHostProjectPath)}) does not exist.`);
            return;
        }
        this.$options["hostProjectPath"] = resolvedHostProjectPath;
        if (args.length > 2) {
            this.$options["hostProjectModuleName"] = args[2];
        }
        return super.execute(args);
    }
    async canExecute(args) {
        const canSuperExecute = await super.canExecute(args);
        if (!canSuperExecute) {
            return false;
        }
        const platform = args[0].toLowerCase();
        if (!args[1]) {
            const hostProjectPath = this.getEmbedConfigForKey("hostProjectPath", platform);
            if (hostProjectPath) {
                args[1] = hostProjectPath;
            }
        }
        if (!args[2]) {
            const hostProjectModuleName = this.getEmbedConfigForKey("hostProjectModuleName", platform);
            if (hostProjectModuleName) {
                args[2] = hostProjectModuleName;
            }
        }
        console.log(args);
        if (args.length < 2) {
            return false;
        }
        return true;
    }
    getEmbedConfigForKey(key, platform) {
        return this.$projectConfigService.getValue(`embed.${platform}.${key}`, this.$projectConfigService.getValue(`embed.${key}`));
    }
}
exports.EmbedCommand = EmbedCommand;
yok_1.injector.registerCommand("embed", EmbedCommand);
