"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 });
const cli_core_1 = require("@igniteui/cli-core");
const opn = require("opn");
const PromptSession_1 = require("../PromptSession");
let doc;
doc = {
    // tslint:disable:object-literal-sort-keys
    command: "doc [term]",
    desc: "opens the Infragistics search for the given term",
    builder: {
        term: {
            describe: "The term you would like to search for",
            type: "string"
        }
    },
    template: null,
    open(target) {
        opn(target);
    },
    execute(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Doc"
            });
            if (!argv.term) {
                const answer = yield PromptSession_1.PromptSession.chooseTerm();
                argv.term = answer;
                yield this.execute(argv);
            }
            else if (!cli_core_1.Util.isAlphanumericExt(argv.term)) {
                return cli_core_1.Util.error(`The search term '${argv.term}' is not valid.` + "\n" +
                    "Name should start with a letter and can also contain numbers, dashes and spaces.", "red");
            }
            else {
                cli_core_1.Util.log(`Review your search results in the browser`, "green");
                cli_core_1.GoogleAnalytics.post({
                    t: "event",
                    ec: "$ig doc",
                    ea: `term to search: ${argv.term}`,
                    cd13: argv.term
                });
                doc.open(`https://www.infragistics.com/search?q=${argv.term.trim()}`);
            }
        });
    }
};
exports.default = doc;
