import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Model, Prop, Watch } from 'vue-property-decorator';
import { AddressField, copyAddress, CountryKey } from '../../../utils/address-lookup/address';
import { ModulVue } from '../../../utils/vue/vue';
import { MDropdown } from '../../dropdown/dropdown';
import { MTextfield } from '../../textfield/textfield';
import WithRender from './address-editor.html';
var MAddressEditor = /** @class */ (function (_super) {
    __extends(MAddressEditor, _super);
    function MAddressEditor() {
        var _a, _b;
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.i18nBuildingNumber = _this.$i18n.translate('m-address-editor:building-number');
        _this.i18nSubBuilding = _this.$i18n.translate('m-address-editor:sub-building');
        _this.i18nStreet = _this.$i18n.translate('m-address-editor:street');
        _this.i18nCity = _this.$i18n.translate('m-address-editor:city');
        _this.i18nCountry = _this.$i18n.translate('m-address-editor:country');
        _this.i18nProvince = _this.$i18n.translate('m-address-editor:province');
        _this.i18nPostalCode = _this.$i18n.translate('m-address-editor:postal-code');
        _this.currentAddress = copyAddress(_this.address);
        _this.errors = (_a = {},
            _a[AddressField.BUILDING_NUMBER] = [],
            _a[AddressField.STREET] = [],
            _a[AddressField.SUB_BUILDING] = [],
            _a[AddressField.CITY] = [],
            _a[AddressField.PROVINCE] = [],
            _a[AddressField.COUNTRY] = [],
            _a[AddressField.POSTAL_CODE] = [],
            _a);
        _this.touched = (_b = {},
            _b[AddressField.BUILDING_NUMBER] = false,
            _b[AddressField.STREET] = false,
            _b[AddressField.SUB_BUILDING] = false,
            _b[AddressField.CITY] = false,
            _b[AddressField.PROVINCE] = false,
            _b[AddressField.COUNTRY] = false,
            _b[AddressField.POSTAL_CODE] = false,
            _b);
        return _this;
    }
    MAddressEditor.prototype.mounted = function () {
        this.isValid();
    };
    MAddressEditor.prototype.onAddressChange = function () {
        this.currentAddress = this.address;
        this.updateCountryCode(this.currentAddress[AddressField.COUNTRY][CountryKey.COUNTRY_ISO2]);
    };
    MAddressEditor.prototype.onProvincesChange = function () {
        this.validate(AddressField.PROVINCE);
    };
    MAddressEditor.prototype.onBuildingNumberChange = function (value) {
        this.touched[AddressField.BUILDING_NUMBER] = true;
        this.validate(AddressField.BUILDING_NUMBER, value);
        this.updateValue(AddressField.BUILDING_NUMBER, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "buildingNumber", {
        get: function () {
            return this.currentAddress.buildingNumber;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "buildingNumberHasError", {
        get: function () {
            return !!this.errors[AddressField.BUILDING_NUMBER] && this.errors[AddressField.BUILDING_NUMBER].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "buildingNumberNextError", {
        get: function () {
            if (this.buildingNumberHasError) {
                return this.errors[AddressField.BUILDING_NUMBER][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onStreetChange = function (value) {
        this.touched[AddressField.STREET] = true;
        this.validate(AddressField.STREET, value);
        this.updateValue(AddressField.STREET, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "street", {
        get: function () {
            return this.currentAddress.street;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "streetHasError", {
        get: function () {
            return !!this.errors[AddressField.STREET] && this.errors[AddressField.STREET].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "streetNextError", {
        get: function () {
            if (this.buildingNumberHasError) {
                return this.errors[AddressField.STREET][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onSubBuildingChange = function (value) {
        this.touched[AddressField.SUB_BUILDING] = true;
        this.validate(AddressField.SUB_BUILDING, value);
        this.updateValue(AddressField.SUB_BUILDING, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "subBuilding", {
        get: function () {
            return this.currentAddress.subBuilding;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "subBuildingHasError", {
        get: function () {
            return !!this.errors[AddressField.SUB_BUILDING] && this.errors[AddressField.SUB_BUILDING].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "subBuildingNextError", {
        get: function () {
            if (this.subBuildingHasError) {
                return this.errors[AddressField.SUB_BUILDING][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onCountryChange = function (value) {
        this.touched[AddressField.COUNTRY] = true;
        this.validate(AddressField.COUNTRY, value);
        if (this.currentAddress.country.countryIso2 !== value) {
            this.clearProvinceValue();
            this.onProvinceChange();
        }
        this.updateCountryValue(value);
        this.validate(AddressField.PROVINCE, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "country", {
        get: function () {
            return this.currentAddress.country.countryIso2;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "countryHasError", {
        get: function () {
            return !!this.errors[AddressField.COUNTRY] && this.errors[AddressField.COUNTRY].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "countryNextError", {
        get: function () {
            if (this.countryHasError) {
                return this.errors[AddressField.COUNTRY][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onProvinceChange = function (value) {
        this.touched[AddressField.PROVINCE] = true;
        this.validate(AddressField.PROVINCE, value);
        this.updateProvinceValue(value);
    };
    MAddressEditor.prototype.clearProvinceValue = function () {
        this.currentAddress.province = undefined;
    };
    Object.defineProperty(MAddressEditor.prototype, "provinceHasError", {
        get: function () {
            return !!this.errors[AddressField.PROVINCE] && this.errors[AddressField.PROVINCE].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "provinceNextError", {
        get: function () {
            if (this.provinceHasError) {
                return this.errors[AddressField.PROVINCE][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "province", {
        get: function () {
            return (this.currentAddress.province) ? this.currentAddress.province.provinceCode : '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onPostalCodeChange = function (value) {
        this.touched[AddressField.POSTAL_CODE] = true;
        this.validate(AddressField.POSTAL_CODE, value);
        this.updateValue(AddressField.POSTAL_CODE, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "postalCode", {
        get: function () {
            return this.currentAddress.postalCode;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "postalCodeHasError", {
        get: function () {
            return !!this.errors[AddressField.POSTAL_CODE] && this.errors[AddressField.POSTAL_CODE].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "postalCodeNextError", {
        get: function () {
            if (this.postalCodeHasError) {
                return this.errors[AddressField.POSTAL_CODE][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.onCityChange = function (value) {
        this.touched[AddressField.CITY] = true;
        this.validate(AddressField.CITY, value);
        this.updateValue(AddressField.CITY, value);
    };
    Object.defineProperty(MAddressEditor.prototype, "city", {
        get: function () {
            return this.currentAddress.city;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "cityHasError", {
        get: function () {
            return !!this.errors[AddressField.CITY] && this.errors[AddressField.CITY].length > 0;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MAddressEditor.prototype, "cityNextError", {
        get: function () {
            if (this.cityHasError) {
                return this.errors[AddressField.CITY][0];
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    MAddressEditor.prototype.updateCountryCode = function (_code) { };
    MAddressEditor.prototype.updateAddress = function (_address) { };
    MAddressEditor.prototype.updateIsValid = function (isValid) { };
    MAddressEditor.prototype.isValid = function () {
        var _this = this;
        this.updateIsValid(Object.keys(this.errors)
            .map(function (key) { return _this.errors[key].length; })
            .reduce(function (acc, numberErrors) { return (numberErrors > 0) ? false : acc; }, true));
    };
    MAddressEditor.prototype.updateValue = function (field, value) {
        if (this.currentAddress[field] !== undefined
            && (!this.errors[field]
                || (this.errors[field] && this.errors[field].length === 0))) {
            this.currentAddress[field] = value;
            this.updateAddress(this.currentAddress);
        }
    };
    MAddressEditor.prototype.updateCountryValue = function (value) {
        if (this.currentAddress[AddressField.COUNTRY] !== undefined
            && (!this.errors[AddressField.COUNTRY]
                || (this.errors[AddressField.COUNTRY]
                    && this.errors[AddressField.COUNTRY].length === 0))) {
            var country = this.countries.find(function (country) { return country.countryIso2 === value; });
            if (country !== undefined) {
                this.currentAddress[AddressField.COUNTRY] = country;
                this.updateCountryCode(country.countryIso2);
                this.updateAddress(this.currentAddress);
            }
        }
    };
    MAddressEditor.prototype.updateProvinceValue = function (value) {
        var province = this.provinces.find(function (province) { return province.provinceCode === value; });
        this.currentAddress[AddressField.PROVINCE] = province;
        this.updateAddress(this.currentAddress);
        this.validate(AddressField.PROVINCE, '');
    };
    MAddressEditor.prototype.validate = function (field, value) {
        var _this = this;
        if (this.validations[field]) {
            this.errors[field] = this.validations[field]
                .reduce(function (acc, validation) {
                if (field === AddressField.PROVINCE) {
                    acc.push(validation(value, _this.currentAddress, _this.provinces));
                }
                else {
                    acc.push(validation(value, _this.currentAddress));
                }
                return acc;
            }, [])
                .filter(function (value) { return value !== ''; });
        }
        else {
            this.errors[field] = [];
        }
        this.isValid();
    };
    __decorate([
        Model('change')
    ], MAddressEditor.prototype, "address", void 0);
    __decorate([
        Prop({
            required: true,
            validator: function (value) { return value !== undefined; }
        })
    ], MAddressEditor.prototype, "countries", void 0);
    __decorate([
        Prop({
            default: function () { return ([]); }
        })
    ], MAddressEditor.prototype, "provinces", void 0);
    __decorate([
        Prop({ default: function () { return ({}); } })
    ], MAddressEditor.prototype, "validations", void 0);
    __decorate([
        Watch('address')
    ], MAddressEditor.prototype, "onAddressChange", null);
    __decorate([
        Watch('province')
    ], MAddressEditor.prototype, "onProvincesChange", null);
    __decorate([
        Emit('country-change')
    ], MAddressEditor.prototype, "updateCountryCode", null);
    __decorate([
        Emit('change')
    ], MAddressEditor.prototype, "updateAddress", null);
    __decorate([
        Emit('is-valid')
    ], MAddressEditor.prototype, "updateIsValid", null);
    MAddressEditor = __decorate([
        WithRender,
        Component({
            components: {
                MTextfield: MTextfield,
                MDropdown: MDropdown
            }
        })
    ], MAddressEditor);
    return MAddressEditor;
}(ModulVue));
export default MAddressEditor;
//# sourceMappingURL=address-editor.js.map