"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaceAutocomplete = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var custom_logger_1 = require("../../modules/custom-logger");
var config_1 = require("../../config");
var ui_internal_1 = require("../../utils/ui-internal");
var aurelia_i18n_1 = require("aurelia-i18n");
var PlaceAutocomplete = exports.PlaceAutocomplete = (function () {
    function PlaceAutocomplete(element, logger, envConfig, i18n) {
        this.element = element;
        this.logger = logger;
        this.i18n = i18n;
        if ((envConfig === null || envConfig === void 0 ? void 0 : envConfig.globalConfig.googleApiKey) == null && logger.LogError != null) {
            logger.LogError('Google API Key not found; Set config.googleApiKey !', null, 'Datacontext', false);
        }
        this.GOOGLE_API_KEY = envConfig.globalConfig.googleApiKey;
        this.originalConsoleError = console.error;
    }
    PlaceAutocomplete.prototype.attached = function () {
        if (this.GOOGLE_API_KEY == null)
            return;
        if (this.element.tagName === 'INPUT') {
            this.inputElement = this.element;
        }
        else if (this.element.tagName === 'UI-INPUT') {
            var inp = this.element.getElementsByTagName('input');
            if (inp != null && inp.length)
                this.inputElement = inp.item(0);
        }
        if (this.inputElement == null) {
            if (this.logger && this.logger.LogError)
                this.logger.LogError('dw-place-autocomplete must be placed on an INPUT or UI-INPUT tag element only.', null, 'Datacontext', false);
            return;
        }
        this.initConfig();
        this.injectScript();
    };
    PlaceAutocomplete.prototype.detached = function () {
        if (this.script != null) {
            this.script.remove();
        }
        console.error = this.originalConsoleError;
    };
    PlaceAutocomplete.prototype.initConfig = function () {
        if (this.config == null) {
            this.config = {};
        }
        if (this.config.country == null) {
            this.config.country = 'be';
        }
        if (this.config.language == null) {
            this.config.language = navigator.language;
        }
    };
    PlaceAutocomplete.prototype.initAutocomplete = function () {
        var _this_1 = this;
        if (this.inputElement != null) {
            var center = { lat: 50.62627, lng: 4.69837 };
            var boxSize = 1.2;
            var defaultBounds = {
                north: center.lat + boxSize,
                south: center.lat - boxSize,
                east: center.lng + boxSize,
                west: center.lng - boxSize,
            };
            this.autocomplete = new google.maps.places.Autocomplete(this.inputElement, {
                componentRestrictions: { country: this.config.country },
                fields: ["address_components", "geometry"],
                types: ["address"],
                bounds: defaultBounds
            });
            if (this.config.focus == true) {
                this.inputElement.focus();
            }
            this.autocomplete.addListener('place_changed', function () {
                var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
                var placeResult = _this_1.autocomplete.getPlace();
                if (placeResult.address_components != null) {
                    var placeComponents = placeResult.address_components;
                    if (placeComponents.length) {
                        _this_1.route = (_a = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'route'; }); })) === null || _a === void 0 ? void 0 : _a.long_name;
                        _this_1.streetNumber = (_b = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'street_number'; }); })) === null || _b === void 0 ? void 0 : _b.long_name;
                        _this_1.locality = (_f = (_d = (_c = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'locality'; }); })) === null || _c === void 0 ? void 0 : _c.long_name) !== null && _d !== void 0 ? _d : (_e = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'sublocality'; }); })) === null || _e === void 0 ? void 0 : _e.long_name) !== null && _f !== void 0 ? _f : (_g = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'postal_town'; }); })) === null || _g === void 0 ? void 0 : _g.long_name;
                        _this_1.postalCode = (_h = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'postal_code'; }); })) === null || _h === void 0 ? void 0 : _h.long_name;
                        _this_1.country = (_j = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'country'; }); })) === null || _j === void 0 ? void 0 : _j.long_name;
                        _this_1.countrycode = (_k = placeComponents.find(function (c) { return c.types.find(function (t) { return t == 'country'; }); })) === null || _k === void 0 ? void 0 : _k.short_name;
                        _this_1.longRoute = _this_1.route;
                        if (_this_1.streetNumber != null) {
                            _this_1.longRoute += ', ' + _this_1.streetNumber;
                        }
                        if (((_l = placeResult.geometry) === null || _l === void 0 ? void 0 : _l.location) != null) {
                            _this_1.latitude = placeResult.geometry.location.lat();
                            _this_1.longitude = placeResult.geometry.location.lng();
                        }
                        _this_1.completeModel();
                        _this_1.inputElement.value = null;
                    }
                }
            });
            var _this_2 = this;
            console.error = function () {
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                _this_2.originalConsoleError.apply(console, args);
                if (args.length > 0 && args[0].toString().includes("You have exceeded your daily request quota for this API")) {
                    _this_2.logger.LogError(_this_2.i18n.tr("general.errorLimitExceed"), args[0], 'PlaceAutocomplete', true);
                }
            };
        }
    };
    PlaceAutocomplete.prototype.completeModel = function () {
        var model = {};
        model.route = this.route;
        model.streetNumber = this.streetNumber;
        model.longRoute = this.longRoute;
        model.postalCode = this.postalCode;
        model.locality = this.locality;
        model.country = this.country;
        model.countryCode = this.countrycode;
        model.latitude = this.latitude;
        model.longitude = this.longitude;
        this.model = model;
    };
    PlaceAutocomplete.prototype.injectScript = function () {
        var _this_1 = this;
        if (this.inputElement) {
            var doc = this.inputElement.ownerDocument;
            this.script = doc.createElement('script');
            this.script.setAttribute('src', "https://maps.googleapis.com/maps/api/js?key=".concat(this.GOOGLE_API_KEY, "&libraries=places&v=weekly&language=").concat(this.config.language));
            this.script.async = true;
            this.script.onload = function (d) {
                ui_internal_1.UIInternal.queueTask(function () {
                    _this_1.initAutocomplete();
                });
            };
            doc.body.after(this.script);
        }
    };
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "route", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "longRoute", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "streetNumber", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "locality", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "postalCode", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "country", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", String)
    ], PlaceAutocomplete.prototype, "countrycode", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", Number)
    ], PlaceAutocomplete.prototype, "latitude", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.fromView }),
        __metadata("design:type", Number)
    ], PlaceAutocomplete.prototype, "longitude", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Object)
    ], PlaceAutocomplete.prototype, "model", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Object)
    ], PlaceAutocomplete.prototype, "config", void 0);
    PlaceAutocomplete = __decorate([
        aurelia_framework_1.autoinject,
        (0, aurelia_framework_1.customAttribute)('dw-place-autocomplete'),
        __metadata("design:paramtypes", [Element, custom_logger_1.CustomLogger, config_1.Config, aurelia_i18n_1.I18N])
    ], PlaceAutocomplete);
    return PlaceAutocomplete;
}());

//# sourceMappingURL=place-autocomplete.js.map
