"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());
    });
};
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) === ".") {
            // resolve relative to the project dir
            const projectDir = this.$projectData.projectDir;
            return (0, path_1.resolve)(projectDir, hostProjectPath);
        }
        return (0, path_1.resolve)(hostProjectPath);
    }
    execute(args) {
        const _super = Object.create(null, {
            execute: { get: () => super.execute }
        });
        return __awaiter(this, void 0, void 0, function* () {
            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.call(this, args);
        });
    }
    canExecute(args) {
        const _super = Object.create(null, {
            canExecute: { get: () => super.canExecute }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const canSuperExecute = yield _super.canExecute.call(this, args);
            if (!canSuperExecute) {
                return false;
            }
            // args[0] is the platform
            // args[1] is the path to the host project
            // args[2] is the host project module name
            const platform = args[0].toLowerCase();
            // also allow these to be set in the nativescript.config.ts
            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) {
        // get the embed.<platform>.<key> value, or fallback to embed.<key> value
        return this.$projectConfigService.getValue(`embed.${platform}.${key}`, this.$projectConfigService.getValue(`embed.${key}`));
    }
}
exports.EmbedCommand = EmbedCommand;
yok_1.injector.registerCommand("embed", EmbedCommand);
//# sourceMappingURL=embed.js.map