import { AddressSources } from './address';
var KEY_ADDRESS_TYPE = 'address';
var GOOGLE_ADDRESS_COMPONENTS;
(function (GOOGLE_ADDRESS_COMPONENTS) {
    GOOGLE_ADDRESS_COMPONENTS["STREET_NUMBER"] = "street_number";
    GOOGLE_ADDRESS_COMPONENTS["ROUTE"] = "route";
    GOOGLE_ADDRESS_COMPONENTS["LOCALITY"] = "locality";
    GOOGLE_ADDRESS_COMPONENTS["POSTAL_CODE"] = "postal_code";
    GOOGLE_ADDRESS_COMPONENTS["ADMINISTRATIVE_AREA"] = "administrative_area_level_1";
    GOOGLE_ADDRESS_COMPONENTS["COUNTRY"] = "country";
})(GOOGLE_ADDRESS_COMPONENTS || (GOOGLE_ADDRESS_COMPONENTS = {}));
var FindResponse = /** @class */ (function () {
    function FindResponse() {
    }
    return FindResponse;
}());
export { FindResponse };
var RetrieveResponse = /** @class */ (function () {
    function RetrieveResponse() {
    }
    return RetrieveResponse;
}());
export { RetrieveResponse };
var AddressLookupToAddressSummary = /** @class */ (function () {
    function AddressLookupToAddressSummary() {
    }
    AddressLookupToAddressSummary.prototype.mapLoqate = function (response) {
        if (!response.request || !response.result) {
            return undefined;
        }
        var type = (response.result.Type || '').toLocaleLowerCase();
        return {
            queryInput: response.request.Text,
            value: response.result.Id,
            type: type,
            label: response.result.Text,
            description: this.formatHTMLDescription(response)
        };
    };
    AddressLookupToAddressSummary.prototype.mapGoogle = function (response) {
        if (!response.request || !response.result) {
            return undefined;
        }
        var type = KEY_ADDRESS_TYPE;
        return {
            queryInput: response.request.input,
            value: response.result.place_id,
            type: type,
            label: response.result.structured_formatting.main_text,
            description: response.result.structured_formatting.secondary_text
        };
    };
    AddressLookupToAddressSummary.prototype.formatHTMLDescription = function (response) {
        if (!response.request || !response.result) {
            return undefined;
        }
        if ((response.result.Type || '').toLocaleLowerCase() === KEY_ADDRESS_TYPE) {
            return response.result.Description;
        }
        var parts = response.result.Description.split('-');
        if (parts.length === 1) {
            return parts[0];
        }
        var lastPart = parts.pop();
        if (!lastPart) {
            return '';
        }
        return parts + ("<span class=\"m-address-lookup-field__results\">" + lastPart + "</span>");
    };
    return AddressLookupToAddressSummary;
}());
export { AddressLookupToAddressSummary };
var AddressRetrieveToAddress = /** @class */ (function () {
    function AddressRetrieveToAddress() {
    }
    AddressRetrieveToAddress.prototype.mapLoqate = function (response) {
        if (!response.request || !response.result) {
            return undefined;
        }
        return {
            id: response.request.Id,
            name: '',
            buildingNumber: response.result.BuildingNumber,
            street: response.result.Street,
            city: response.result.City,
            postalCode: response.result.PostalCode,
            province: {
                province: response.result.ProvinceName,
                provinceCode: response.result.ProvinceCode
            },
            country: {
                country: response.result.CountryName,
                countryIso2: response.result.CountryIso2
            },
            subBuilding: response.result.SubBuilding,
            formattedAddress: '',
            adrFormattedAddress: '',
            attributions: [],
            source: AddressSources.LOQATE,
            isEstablishment: false
        };
    };
    AddressRetrieveToAddress.prototype.mapGoogle = function (response) {
        // City name isn't correctly translated.
        // https://stackoverflow.com/questions/27567757/inconsistent-language-in-google-place-details-api
        // A workaround exists using textsearch.  Will try it at a later time.
        var _a;
        if (!response.request || !response.result) {
            return undefined;
        }
        var componentsByType = (_a = {},
            _a[GOOGLE_ADDRESS_COMPONENTS.STREET_NUMBER] = { long_name: '', short_name: '', types: [] },
            _a[GOOGLE_ADDRESS_COMPONENTS.ROUTE] = { long_name: '', short_name: '', types: [] },
            _a[GOOGLE_ADDRESS_COMPONENTS.LOCALITY] = { long_name: '', short_name: '', types: [] },
            _a[GOOGLE_ADDRESS_COMPONENTS.POSTAL_CODE] = { long_name: '', short_name: '', types: [] },
            _a[GOOGLE_ADDRESS_COMPONENTS.ADMINISTRATIVE_AREA] = { long_name: '', short_name: '', types: [] },
            _a[GOOGLE_ADDRESS_COMPONENTS.COUNTRY] = { long_name: '', short_name: '', types: [] },
            _a);
        (response.result.address_components || []).forEach(function (component) {
            component.types.forEach(function (type) {
                componentsByType[type] = component;
            });
        });
        return {
            id: response.request.placeId,
            buildingNumber: componentsByType[GOOGLE_ADDRESS_COMPONENTS.STREET_NUMBER].long_name,
            street: componentsByType[GOOGLE_ADDRESS_COMPONENTS.ROUTE].long_name,
            city: componentsByType[GOOGLE_ADDRESS_COMPONENTS.LOCALITY].long_name,
            postalCode: componentsByType[GOOGLE_ADDRESS_COMPONENTS.POSTAL_CODE].long_name,
            province: {
                province: componentsByType[GOOGLE_ADDRESS_COMPONENTS.ADMINISTRATIVE_AREA].long_name,
                provinceCode: componentsByType[GOOGLE_ADDRESS_COMPONENTS.ADMINISTRATIVE_AREA].short_name
            },
            country: {
                country: componentsByType[GOOGLE_ADDRESS_COMPONENTS.COUNTRY].long_name,
                countryIso2: componentsByType[GOOGLE_ADDRESS_COMPONENTS.COUNTRY].short_name
            },
            subBuilding: '',
            formattedAddress: response.result.formatted_address,
            adrFormattedAddress: response.result.adr_address,
            attributions: response.result.html_attributions || [],
            source: AddressSources.GOOGLE_PLACES,
            isEstablishment: !!(response.result.types || []).find(function (type) { return type === 'establishment'; }),
            name: response.result.name
        };
    };
    return AddressRetrieveToAddress;
}());
export { AddressRetrieveToAddress };
//# sourceMappingURL=address-lookup-response-mapper.js.map