import { __awaiter, __generator } from "tslib";
import uuid from '../uuid/uuid';
import { AddressLookupServiceProvider } from './address-lookup';
import { GoogleFindResponseBuilder, GoogleRetrieveResponseBuilder } from './address-lookup-google';
import { AddressLookupToAddressSummary, AddressRetrieveToAddress } from './address-lookup-response-mapper';
var AddressLookupGoogleProxyService = /** @class */ (function () {
    /**
     * Use this service if you have your own proxy to google. A proxy is useful to keep your googleAPI key secret.
     * @param findUrl https://developers.google.com/places/web-service/autocomplete
     * @param retrieveUrl https://developers.google.com/places/web-service/details
     */
    function AddressLookupGoogleProxyService(findPromise, retrievePromise, findErrorCallback, retrieveErrorCallback) {
        this.findPromise = findPromise;
        this.retrievePromise = retrievePromise;
        this.findErrorCallback = findErrorCallback;
        this.retrieveErrorCallback = retrieveErrorCallback;
        this.serviceProvider = AddressLookupServiceProvider.GoogleProxy;
    }
    AddressLookupGoogleProxyService.prototype.find = function (query) {
        return __awaiter(this, void 0, void 0, function () {
            var params, results;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.ensureCreateToken();
                        params = {
                            input: query.input,
                            sessionToken: this.sessionToken
                        };
                        return [4 /*yield*/, this.findPromise({
                                input: params.input,
                                sessionToken: params.sessionToken.toString()
                            })
                                .then(function (response) { return (response.predictions); })
                                .catch(function (error) {
                                _this.findErrorCallback(error);
                                return [];
                            })];
                    case 1:
                        results = _a.sent();
                        return [2 /*return*/, results
                                .map(function (prediction) { return new GoogleFindResponseBuilder()
                                .setRequest(params)
                                .setResult(prediction)
                                .build()
                                .mapTo(new AddressLookupToAddressSummary()); })];
                }
            });
        });
    };
    AddressLookupGoogleProxyService.prototype.retrieve = function (query) {
        return __awaiter(this, void 0, void 0, function () {
            var params, results, address;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        params = {
                            placeId: query.id,
                            sessionToken: this.sessionToken
                        };
                        return [4 /*yield*/, this.retrievePromise({
                                placeId: params.placeId,
                                sessionToken: params.sessionToken.toString()
                            })
                                .then(function (response) { return [response.result]; })
                                .catch(function (error) {
                                _this.retrieveErrorCallback(error);
                                return [];
                            })];
                    case 1:
                        results = _a.sent();
                        this.discardToken();
                        if (results.length > 0) {
                            address = new GoogleRetrieveResponseBuilder()
                                .setRequest(params)
                                .setResult(results[0])
                                .build()
                                .mapTo(new AddressRetrieveToAddress());
                            return [2 /*return*/, [address]];
                        }
                        else {
                            return [2 /*return*/, []];
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    AddressLookupGoogleProxyService.prototype.ensureCreateToken = function () {
        var token = this.sessionToken ? this.sessionToken : uuid.generate();
        this.sessionToken = token;
    };
    AddressLookupGoogleProxyService.prototype.discardToken = function () {
        this.sessionToken = undefined;
    };
    return AddressLookupGoogleProxyService;
}());
export default AddressLookupGoogleProxyService;
//# sourceMappingURL=address-lookup-proxy-google-service.js.map