import { __spreadArrays } from "tslib";
import { ModulVue } from '../vue/vue';
var GoogleAPI = /** @class */ (function () {
    function GoogleAPI(key) {
        var _this = this;
        this.key = key;
        this.CALLBACK_NAME = 'gmapsCallback';
        this.initPromise = new Promise(function (resolve, reject) {
            _this.resolveInitPromise = resolve;
            _this.rejectInitPromise = reject;
        });
        this.initialized = false;
        this.initialized = false;
    }
    Object.defineProperty(GoogleAPI.prototype, "instance", {
        get: function () {
            if (!this.initialized) {
                this.initialize();
            }
            return this.initPromise;
        },
        enumerable: false,
        configurable: true
    });
    GoogleAPI.prototype.promisifyFetch = function (fetchFunction, request) {
        return new Promise(function (resolve, reject) {
            fetchFunction(request, function (results, status) {
                var resultIsArray = results && results.length !== undefined;
                var resultsAsArray = results && resultIsArray ? __spreadArrays(results) : results ? [results] : [];
                switch (status) {
                    case google.maps.places.PlacesServiceStatus.ERROR:
                    case google.maps.places.PlacesServiceStatus.INVALID_REQUEST:
                    case google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT:
                    case google.maps.places.PlacesServiceStatus.NOT_FOUND:
                    case google.maps.places.PlacesServiceStatus.REQUEST_DENIED:
                    case google.maps.places.PlacesServiceStatus.UNKNOWN_ERROR:
                        return reject("Google places API error: " + status);
                    case google.maps.places.PlacesServiceStatus.OK:
                    case google.maps.places.PlacesServiceStatus.ZERO_RESULTS:
                        return resolve(resultsAsArray);
                    default:
                        throw new Error('Unhandled google places service status');
                }
            });
        });
    };
    Object.defineProperty(GoogleAPI.prototype, "placesAutocomplete", {
        get: function () {
            return this.instance
                .then(function () {
                var service = new google.maps.places.AutocompleteService();
                return {
                    getPlacePredictions: service.getPlacePredictions.bind(service),
                    getQueryPredictions: service.getQueryPredictions.bind(service)
                };
            });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(GoogleAPI.prototype, "placesService", {
        get: function () {
            return this.instance
                .then(function () {
                // the elements is never rendered in the DOM.  We have to show attributions manually after fetching an address.
                var element = document.createElement('div');
                var service = new google.maps.places.PlacesService(element);
                return {
                    findPlaceFromPhoneNumber: service.findPlaceFromPhoneNumber.bind(service),
                    findPlaceFromQuery: service.findPlaceFromQuery.bind(service),
                    getDetails: service.getDetails.bind(service),
                    nearbySearch: service.nearbySearch.bind(service),
                    radarSearch: undefined,
                    textSearch: service.textSearch.bind(service)
                };
            });
        },
        enumerable: false,
        configurable: true
    });
    GoogleAPI.prototype.createToken = function () {
        return this.instance
            .then(function () {
            return new google.maps.places.AutocompleteSessionToken();
        });
    };
    GoogleAPI.prototype.initialize = function () {
        var _this = this;
        this.initialized = true;
        // The callback function is called by
        // the Google Maps script if it is
        // successfully loaded.
        window[this.CALLBACK_NAME] = function () { return _this.resolveInitPromise(window.google.maps); };
        // We inject a new script tag into
        // the `<head>` of our HTML to load
        // the Google Maps script.
        var script = document.createElement('script');
        script.async = true;
        script.defer = true;
        var defaultLanguage = ModulVue.prototype.$i18n ? ModulVue.prototype.$i18n.currentLocale : undefined;
        var languageParam = defaultLanguage ? "&language=" + defaultLanguage : '';
        // It's tricky to dynamically change google language at the moment.  https://stackoverflow.com/questions/20090711/google-map-dyanamic-language-change.
        // So if the language change, google language won't change.
        script.src = "https://maps.googleapis.com/maps/api/js?key=" + this.key + languageParam + "&libraries=places&callback=" + this.CALLBACK_NAME;
        script.onerror = this.rejectInitPromise;
        document.querySelector('head').appendChild(script);
    };
    return GoogleAPI;
}());
export default GoogleAPI;
//# sourceMappingURL=google-api.js.map