var _a;
import { __awaiter, __decorate, __extends, __generator } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { InputWidth } from '../../../mixins/input-width/input-width';
import { AddressLookupServiceProvider } from '../../../utils/address-lookup/address-lookup';
import { ModulVue } from '../../../utils/vue/vue';
import { MAutocomplete } from '../../autocomplete/autocomplete';
import { AUTOCOMPLETE_NAME, POWERED_BY_GOOGLE } from '../../component-names';
import { MPoweredByGoogle } from '../powered-by-google/powered-by-google';
import WithRender from './address-lookup-field.html?style=./address-lookup-field.scss';
var KEY_ADDRESS_TYPE = 'address';
var MAddressLookupField = /** @class */ (function (_super) {
    __extends(MAddressLookupField, _super);
    function MAddressLookupField() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.selection = '';
        _this.open = false;
        _this.results = [];
        return _this;
    }
    MAddressLookupField.prototype.clearValue = function () {
        if (!this.value) {
            this.selection = '';
        }
    };
    MAddressLookupField.prototype.onComplete = function (value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.fetchData(value);
                return [2 /*return*/];
            });
        });
    };
    MAddressLookupField.prototype.onSelect = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var currentAddress, results;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        currentAddress = this.results.find(function (result) { return result.value === id; });
                        if (!currentAddress) return [3 /*break*/, 5];
                        if (!(currentAddress.type !== KEY_ADDRESS_TYPE)) return [3 /*break*/, 2];
                        this.open = false;
                        return [4 /*yield*/, this.fetchData(currentAddress.queryInput, id)];
                    case 1:
                        _a.sent();
                        this.open = true;
                        return [3 /*break*/, 3];
                    case 2:
                        this.open = false;
                        _a.label = 3;
                    case 3:
                        if (!(currentAddress.type === KEY_ADDRESS_TYPE && this.selection === currentAddress.value)) return [3 /*break*/, 5];
                        return [4 /*yield*/, this.$addressLookup.retrieve({ id: currentAddress.value })];
                    case 4:
                        results = _a.sent();
                        if (results.length > 0) {
                            this.emitSelection(results[0]);
                        }
                        _a.label = 5;
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    Object.defineProperty(MAddressLookupField.prototype, "i18nSearch", {
        get: function () {
            return this.label || this.$i18n.translate('m-address-lookup-field:search-field');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressLookupField.prototype, "i18nPlaceholder", {
        get: function () {
            return this.placeholder || this.$i18n.translate('m-address-lookup-field:placeholder');
        },
        enumerable: false,
        configurable: true
    });
    MAddressLookupField.prototype.getClassToggles = function (address) {
        return {
            'm-address-lookup-field__item-address': address.type === KEY_ADDRESS_TYPE || address.type === undefined,
            'm-address-lookup-field__item-expandable': address.type !== KEY_ADDRESS_TYPE
        };
    };
    MAddressLookupField.prototype.emitSelection = function (_currentAddress) {
    };
    Object.defineProperty(MAddressLookupField.prototype, "googleIsUsed", {
        get: function () {
            if (this.$addressLookup) {
                return this.$addressLookup.serviceProvider === AddressLookupServiceProvider.Google ||
                    this.$addressLookup.serviceProvider === AddressLookupServiceProvider.GoogleProxy;
            }
            else {
                return false;
            }
        },
        enumerable: false,
        configurable: true
    });
    MAddressLookupField.prototype.fetchData = function (value, id) {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this;
                        return [4 /*yield*/, this.$addressLookup.find({
                                input: value,
                                id: id,
                                origin: this.origin,
                                language: this.language
                            })];
                    case 1:
                        _a.results = _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "value", void 0);
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "origin", void 0);
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "language", void 0);
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "errorMessage", void 0);
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "label", void 0);
    __decorate([
        Prop()
    ], MAddressLookupField.prototype, "placeholder", void 0);
    __decorate([
        Watch('value', { deep: true, immediate: true })
    ], MAddressLookupField.prototype, "clearValue", null);
    __decorate([
        Emit('address-retrieved'),
        Emit('input')
    ], MAddressLookupField.prototype, "emitSelection", null);
    MAddressLookupField = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[AUTOCOMPLETE_NAME] = MAutocomplete,
                _a[POWERED_BY_GOOGLE] = MPoweredByGoogle,
                _a),
            mixins: [
                InputWidth
            ]
        })
    ], MAddressLookupField);
    return MAddressLookupField;
}(ModulVue));
export { MAddressLookupField };
//# sourceMappingURL=address-lookup-field.js.map