import { __awaiter, __generator } from "tslib";
import { AddressLookupServiceProvider } from './address-lookup';
import { GoogleFindResponseBuilder, GoogleRetrieveResponseBuilder } from './address-lookup-google';
import { AddressLookupToAddressSummary, AddressRetrieveToAddress } from './address-lookup-response-mapper';
import GoogleAPI from './google-api';
var AddressLookupGoogleService = /** @class */ (function () {
    function AddressLookupGoogleService(axios, key) {
        this.axios = axios;
        this.key = key;
        this.serviceProvider = AddressLookupServiceProvider.Google;
        this.googleAPI = new GoogleAPI(this.key);
    }
    AddressLookupGoogleService.prototype.find = function (query) {
        return __awaiter(this, void 0, void 0, function () {
            var request, results, _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0: return [4 /*yield*/, this.ensureCreateToken()];
                    case 1:
                        _c.sent();
                        request = {
                            input: query.input,
                            sessionToken: this.sessionToken
                        };
                        _b = (_a = this.googleAPI).promisifyFetch;
                        return [4 /*yield*/, this.googleAPI.placesAutocomplete];
                    case 2: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).getPlacePredictions, request])];
                    case 3:
                        results = _c.sent();
                        return [2 /*return*/, results
                                .map(function (prediction) { return new GoogleFindResponseBuilder()
                                .setRequest(request)
                                .setResult(prediction)
                                .build()
                                .mapTo(new AddressLookupToAddressSummary()); })];
                }
            });
        });
    };
    AddressLookupGoogleService.prototype.retrieve = function (query) {
        return __awaiter(this, void 0, void 0, function () {
            var request, results, _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0: return [4 /*yield*/, this.ensureCreateToken()];
                    case 1:
                        _c.sent();
                        request = {
                            placeId: query.id,
                            // Use only Basic Data: https://developers.google.com/places/web-service/usage-and-billing#basic-data
                            fields: ['address_component', 'adr_address', 'formatted_address', 'name', 'type'],
                            sessionToken: this.sessionToken
                        };
                        _b = (_a = this.googleAPI).promisifyFetch;
                        return [4 /*yield*/, this.googleAPI.placesService];
                    case 2: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).getDetails, request])];
                    case 3:
                        results = _c.sent();
                        this.discardToken();
                        return [2 /*return*/, results
                                .map(function (prediction) { return new GoogleRetrieveResponseBuilder()
                                .setRequest(request)
                                .setResult(prediction)
                                .build()
                                .mapTo(new AddressRetrieveToAddress()); })];
                }
            });
        });
    };
    AddressLookupGoogleService.prototype.ensureCreateToken = function () {
        return __awaiter(this, void 0, void 0, function () {
            var token, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (!!this.sessionToken) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.googleAPI.createToken()];
                    case 1:
                        _a = _b.sent();
                        return [3 /*break*/, 3];
                    case 2:
                        _a = this.sessionToken;
                        _b.label = 3;
                    case 3:
                        token = _a;
                        this.sessionToken = token;
                        return [2 /*return*/];
                }
            });
        });
    };
    AddressLookupGoogleService.prototype.discardToken = function () {
        this.sessionToken = undefined;
    };
    return AddressLookupGoogleService;
}());
export default AddressLookupGoogleService;
//# sourceMappingURL=address-lookup-google-service.js.map