"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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.CompanyInsightsController = void 0;
const constants_1 = require("../constants");
const decorators_1 = require("../common/decorators");
const util = require("util");
const _ = require("lodash");
const yok_1 = require("../common/yok");
class CompanyInsightsController {
    constructor($config, $httpClient, $ipService, $logger) {
        this.$config = $config;
        this.$httpClient = $httpClient;
        this.$ipService = $ipService;
        this.$logger = $logger;
    }
    getCompanyData() {
        return __awaiter(this, void 0, void 0, function* () {
            let companyData = null;
            let currentPublicIP = null;
            try {
                currentPublicIP = yield this.$ipService.getCurrentIPv4Address();
            }
            catch (err) {
                this.$logger.trace(`Unable to get current public ip address. Error is: `, err);
            }
            if (currentPublicIP) {
                companyData = yield this.getCompanyDataFromPlaygroundInsightsEndpoint(currentPublicIP);
            }
            return companyData;
        });
    }
    getCompanyDataFromPlaygroundInsightsEndpoint(ipAddress) {
        return __awaiter(this, void 0, void 0, function* () {
            let companyData = null;
            try {
                const url = util.format(this.$config.INSIGHTS_URL_ENDPOINT, encodeURIComponent(ipAddress));
                const response = yield this.$httpClient.httpRequest(url);
                const data = JSON.parse(response.body);
                if (data.company) {
                    const industries = _.isArray(data.company.industries)
                        ? data.company.industries.join(constants_1.AnalyticsEventLabelDelimiter)
                        : null;
                    companyData = {
                        name: data.company.name,
                        country: data.company.country,
                        revenue: data.company.revenue,
                        employeeCount: data.company.employeeCount,
                        industries,
                    };
                }
            }
            catch (err) {
                this.$logger.trace(`Unable to get data for company. Error is: ${err}`);
            }
            return companyData;
        });
    }
}
__decorate([
    (0, decorators_1.cache)()
], CompanyInsightsController.prototype, "getCompanyDataFromPlaygroundInsightsEndpoint", null);
exports.CompanyInsightsController = CompanyInsightsController;
yok_1.injector.register("companyInsightsController", CompanyInsightsController);
