import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Model, Prop, Watch } from 'vue-property-decorator';
import { AddressField, copyAddress } from '../../../utils/address-lookup/address';
import { ModulVue } from '../../../utils/vue/vue';
import { MInplaceEdit } from '../../inplace-edit/inplace-edit';
import MAddressEditor from '../address-editor/address-editor';
import { MAddressLookupField } from '../address-lookup-field/address-lookup-field';
import { MAddressReader } from '../address-reader/address-reader';
import WithRender from './address-autocomplete-field.html';
var MAddressAutocompleteField = /** @class */ (function (_super) {
    __extends(MAddressAutocompleteField, _super);
    function MAddressAutocompleteField() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.editAddress = _this.address;
        _this.editing = false;
        _this.error = '';
        _this.editFormTitle = 'Edit address !translation';
        _this.currentProvinces = [];
        _this.current = copyAddress(_this.address);
        _this.canSubmit = true;
        return _this;
    }
    MAddressAutocompleteField.prototype.mounted = function () {
        if (this.address && this.address.country) {
            this.onCountryChange(this.address.country.countryIso2);
        }
    };
    MAddressAutocompleteField.prototype.onAddressChange = function () {
        this.current = copyAddress(this.address);
        if (this.current && this.current.country) {
            this.onCountryChange(this.current.country.countryIso2);
        }
    };
    MAddressAutocompleteField.prototype.onRetrieve = function (address) {
        this.current = address;
        this.updateAddress(this.current);
    };
    MAddressAutocompleteField.prototype.onConfirm = function () {
        if (this.canSubmit) {
            this.updateAddress(this.editAddress);
            this.current = copyAddress(this.editAddress);
            this.editing = false;
        }
    };
    MAddressAutocompleteField.prototype.onCancel = function () {
        this.updateAddress(this.currentAddress);
        this.editing = false;
    };
    MAddressAutocompleteField.prototype.onClick = function () {
        if (!this.editing) {
            this.editAddress = copyAddress(this.current);
        }
        this.editing = (this.editing) ? this.editing : !this.editing;
    };
    MAddressAutocompleteField.prototype.onIsValid = function (value) {
        this.canSubmit = value;
    };
    MAddressAutocompleteField.prototype.onCountryChange = function (code) {
        this.currentProvinces = this.provinces[code];
    };
    MAddressAutocompleteField.prototype.updateAddress = function (_address) { };
    Object.defineProperty(MAddressAutocompleteField.prototype, "currentAddress", {
        get: function () {
            return this.current;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressAutocompleteField.prototype, "hasValue", {
        get: function () {
            var _this = this;
            return Object.keys(this.current).map(function (key) { return key !== AddressField.COUNTRY && _this.current[key] !== '' && _this.current[key] !== undefined; })
                .reduce(function (acc, data) { return (data) ? data : acc; }, false);
        },
        enumerable: false,
        configurable: true
    });
    __decorate([
        Model('change')
    ], MAddressAutocompleteField.prototype, "address", void 0);
    __decorate([
        Prop() // used by the lookup field to determine default response language
    ], MAddressAutocompleteField.prototype, "language", void 0);
    __decorate([
        Prop() // used by the lookup field to determine origine for the query
    ], MAddressAutocompleteField.prototype, "origin", void 0);
    __decorate([
        Prop() // filters to apply when displaying address
    ], MAddressAutocompleteField.prototype, "filters", void 0);
    __decorate([
        Prop() // value to use from address sub-object country
    ], MAddressAutocompleteField.prototype, "countryKey", void 0);
    __decorate([
        Prop() // value to use from address sub-object province
    ], MAddressAutocompleteField.prototype, "provinceKey", void 0);
    __decorate([
        Prop() // list of countries for use in dropdown of edit form
    ], MAddressAutocompleteField.prototype, "countries", void 0);
    __decorate([
        Prop() // list of provinces for use in dropdown of edit form
    ], MAddressAutocompleteField.prototype, "provinces", void 0);
    __decorate([
        Prop({ default: function () { return ({}); } }) // validators to apply on form
    ], MAddressAutocompleteField.prototype, "validations", void 0);
    __decorate([
        Watch('address')
    ], MAddressAutocompleteField.prototype, "onAddressChange", null);
    __decorate([
        Emit('change')
    ], MAddressAutocompleteField.prototype, "updateAddress", null);
    MAddressAutocompleteField = __decorate([
        WithRender,
        Component({
            components: {
                MAddressLookupField: MAddressLookupField,
                MAddressReader: MAddressReader,
                MAddressEditor: MAddressEditor,
                MInplaceEdit: MInplaceEdit
            }
        })
    ], MAddressAutocompleteField);
    return MAddressAutocompleteField;
}(ModulVue));
export default MAddressAutocompleteField;
//# sourceMappingURL=address-autocomplete-field.js.map