import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { DecimalPipe } from '@angular/common';
import { ServerSideFieldModel } from '../../services/ServerSideFieldModel';
import { I18nService } from '../.././services/system/locale/I18n';
import { BehaviorSubject } from 'rxjs/Rx';
import { LoggerService } from 'dotcms-js/dotcms-js';
// tslint:disable-next-line:no-unused-variable
var UNITS = {
    km: {
        toKm: function (len) { return len; },
        toMeters: function (len) { return len * 1000; },
        toMiles: function (len) { return len / 1.60934; }
    },
    m: {
        toKm: function (len) { return len / 1000; },
        toMeters: function (len) { return len; },
        toMiles: function (len) { return len / 1609.34; }
    },
    mi: {
        toKm: function (len) { return len / 1.60934; },
        toMeters: function (len) { return len * 1609.34; },
        toMiles: function (len) { return len; }
    }
};
var I8N_BASE = 'api.sites.ruleengine';
var VisitorsLocationContainer = (function () {
    function VisitorsLocationContainer(resources, decimalPipe, loggerService) {
        this.resources = resources;
        this.decimalPipe = decimalPipe;
        this.loggerService = loggerService;
        this.parameterValuesChange = new EventEmitter(false);
        this.circle$ = new BehaviorSubject({
            center: { lat: 38.89, lng: -77.04 },
            radius: 10000
        });
        this.preferredUnit = 'm';
        this.lat = 0;
        this.lng = 0;
        this.radius = 50000;
        this.comparisonValue = 'within';
        this.fromLabel = 'of';
        resources.get(I8N_BASE).subscribe(function (rsrc) { });
        this._rsrcCache = {};
        this.circle$.subscribe(function (e) { }, function (e) {
            loggerService.error('VisitorsLocationContainer', 'Error updating area', e);
        }, function () { });
    }
    VisitorsLocationContainer.prototype.rsrc = function (subkey) {
        var x = this._rsrcCache[subkey];
        if (!x) {
            x = this.resources.get(subkey);
            this._rsrcCache[subkey] = x;
        }
        return x;
    };
    VisitorsLocationContainer.prototype.ngOnChanges = function (change) {
        var _this = this;
        if (change.componentInstance && this.componentInstance != null) {
            var temp = this.componentInstance.parameters;
            var params = temp;
            var comparisonDef = this.componentInstance.parameterDefs['comparison'];
            var opts_1 = comparisonDef.inputType['options'];
            var i18nBaseKey = comparisonDef.i18nBaseKey || this.componentInstance.type.i18nKey;
            var rsrcKey_1 = i18nBaseKey + '.inputs.comparison.';
            var optsAry = Object.keys(opts_1).map(function (key) {
                var sOpt = opts_1[key];
                return {
                    value: sOpt.value,
                    label: _this.rsrc(rsrcKey_1 + sOpt.i18nKey),
                    icon: sOpt.icon
                };
            });
            this.comparisonValue = params.comparison.value || comparisonDef.defaultValue;
            this.comparisonOptions = optsAry;
            this.comparisonControl = ServerSideFieldModel.createNgControl(this.componentInstance, 'comparison');
            this.lat = parseFloat(params.latitude.value) || this.lat;
            this.lng = parseFloat(params.longitude.value) || this.lng;
            this.radius = parseFloat(params.radius.value) || 50000;
            this.preferredUnit =
                params.preferredDisplayUnits.value ||
                    this.componentInstance.parameterDefs['preferredDisplayUnits'].defaultValue;
            this.circle$.next({ center: { lat: this.lat, lng: this.lng }, radius: this.radius });
        }
    };
    VisitorsLocationContainer.prototype.onComparisonChange = function (value) {
        this.parameterValuesChange.emit([{ name: 'comparison', value: value }]);
    };
    VisitorsLocationContainer.prototype.onUpdate = function (circle) {
        this.loggerService.info('App', 'onUpdate', circle);
        this.parameterValuesChange.emit([
            { name: 'latitude', value: circle.center.lat + '' },
            { name: 'longitude', value: circle.center.lng + '' },
            { name: 'radius', value: circle.radius + '' }
        ]);
        this.lat = circle.center.lat;
        this.lng = circle.center.lng;
        this.radius = circle.radius;
        this.circle$.next(circle);
    };
    return VisitorsLocationContainer;
}());
export { VisitorsLocationContainer };
VisitorsLocationContainer.decorators = [
    { type: Component, args: [{
                changeDetection: ChangeDetectionStrategy.OnPush,
                providers: [DecimalPipe],
                selector: 'cw-visitors-location-container',
                template: "<cw-visitors-location-component\n    [circle]=\"circle$ | async\"\n    [preferredUnit]=\"preferredUnit\"\n    [comparisonValue]=\"comparisonValue\"\n    [comparisonControl]=\"comparisonControl\"\n    [comparisonOptions]=\"comparisonOptions\"\n    [fromLabel]=\"fromLabel\"\n    (comparisonChange)=\"onComparisonChange($event)\"\n    (areaChange)=\"onUpdate($event)\"\n></cw-visitors-location-component>\n"
            },] },
];
/** @nocollapse */
VisitorsLocationContainer.ctorParameters = function () { return [
    { type: I18nService, },
    { type: DecimalPipe, },
    { type: LoggerService, },
]; };
VisitorsLocationContainer.propDecorators = {
    'componentInstance': [{ type: Input },],
    'parameterValuesChange': [{ type: Output },],
};
//# sourceMappingURL=visitors-location.container.js.map