import { Component, Directive, ElementRef, HostListener, Input, ViewEncapsulation } from '@angular/core';
import { AfdPceService } from '../services/afd.pce.service';
// import {Countries} from '../data/countries';
import { Countries } from '../data/countries';
import Phone from 'awesome-phonenumber';
import { NG_ASYNC_VALIDATORS, NG_VALIDATORS } from '@angular/forms';
import { map } from 'rxjs/operators';
import intlTelInput from 'intl-tel-input';
import 'intl-tel-input/build/css/intlTelInput.css';
export class AfdPhoneRegexValidDirective {
    constructor(afdPceService, el) {
        this.afdPceService = afdPceService;
        this.el = el;
    }
    ngOnInit() {
        this.id = typeof this.id === 'undefined' ? 'default' : this.id;
        if (this.id === 'default' && this.afdPceService.iti[this.id] !== 'undefined') {
            console.warn(''
                + 'AFD: It appears that you are using multiple phone fields in your application but not setting the "id" parameter.'
                + '"id" is required when using AFD validation on multiple fields');
        }
        if (this.id !== 'default' && this.afdPceService.iti[this.id] !== 'undefined') {
            console.warn(''
                + 'AFD: It appears that the "id" for the phone control you are using AFD validation on is not unique. '
                + 'For the validation to run correctly it must be unique.');
        }
        this.afdPceService.iti[this.id] = intlTelInput(this.el.nativeElement, itiSettings(this.country, this.excludeCountries, this.onlyCountries, this.preferredCountries));
        this.iti = this.afdPceService.iti[this.id];
    }
    // https://angular.io/guide/form-validation#custom-validators
    validate(control) {
        // only try to validate if there is a value in the input
        if (control.value) {
            const { phoneNumber, countryData, numberType, numberTypes } = itiVars(this.iti);
            // swap double zero for plus
            if (control.value === '00') {
                this.el.nativeElement.value = '+';
            }
            // remove country code from input
            if (typeof countryData.dialCode !== 'undefined' && phoneNumber.indexOf(countryData.dialCode) === 1) {
                this.el.nativeElement.value = phoneNumber.substr(countryData.dialCode.length + 1);
            }
            this.el.nativeElement.value = window['intlTelInputUtils']['formatNumber'](this.el.nativeElement.value, countryData.iso2, 'NATIONAL');
            this.afdPceService.phoneCountry = setCountry(countryData.iso2);
            // send useful information to the pce service
            this.afdPceService.phoneInfoSource.next({
                pceValid: false,
                syntaxValid: this.iti.isValidNumber(),
                regexValid: this.iti.isValidNumber(),
                country: this.afdPceService.phoneCountry,
                isMobile: numberTypes[numberType] === 'MOBILE',
                isLandLine: numberTypes[numberType] === 'FIXED_LINE',
                numberType: numberTypes[numberType],
                phoneNumber: this.iti.getNumber(),
                phoneNumberE164: this.iti.getNumber(),
                phoneNumberInternational: this.iti.getNumber(1),
                phoneNumberNational: this.iti.getNumber(2),
                phoneNumberRfc3966: this.iti.getNumber(3)
            });
            // validate syntax
            return this.iti.isValidNumber() ? null : { afdPhoneRegexValid: { message: this.afdPceService.settings.messages.phone.invalidRegex } };
        }
        else {
            // if there is nothing in the control
            return null;
        }
    }
}
AfdPhoneRegexValidDirective.decorators = [
    { type: Component, args: [{
                selector: ' input[afdPhoneRegexValid], input[afdPhoneSyntaxValid]',
                template: '<ng-content></ng-content>',
                encapsulation: ViewEncapsulation.None,
                providers: [
                    {
                        provide: NG_VALIDATORS,
                        useExisting: AfdPhoneRegexValidDirective,
                        multi: true
                    }
                ],
                styles: [".iti{position:relative;display:inline-block}.iti *{box-sizing:border-box;-moz-box-sizing:border-box}.iti__hide{display:none}.iti__v-hide{visibility:hidden}.iti input,.iti input[type=text],.iti input[type=tel]{position:relative;z-index:0;margin-top:0!important;margin-bottom:0!important;padding-right:36px;margin-right:0}.iti__flag-container{position:absolute;top:0;bottom:0;right:0;padding:1px}.iti__selected-flag{z-index:1;position:relative;display:flex;align-items:center;height:100%;padding:0 6px 0 8px}.iti__arrow{margin-left:6px;width:0;height:0;border-left:3px solid transparent;border-right:3px solid transparent;border-top:4px solid #555}.iti__arrow--up{border-top:none;border-bottom:4px solid #555}.iti__country-list{position:absolute;z-index:2;list-style:none;text-align:left;padding:0;margin:0 0 0 -1px;box-shadow:1px 1px 4px #0003;background-color:#fff;border:1px solid #CCC;white-space:nowrap;max-height:200px;overflow-y:scroll;-webkit-overflow-scrolling:touch}.iti__country-list--dropup{bottom:100%;margin-bottom:-1px}@media (max-width: 500px){.iti__country-list{white-space:normal}}.iti__flag-box{display:inline-block;width:20px}.iti__divider{padding-bottom:5px;margin-bottom:5px;border-bottom:1px solid #CCC}.iti__country{padding:5px 10px;outline:none}.iti__dial-code{color:#999}.iti__country.iti__highlight{background-color:#0000000d}.iti__flag-box,.iti__country-name,.iti__dial-code{vertical-align:middle}.iti__flag-box,.iti__country-name{margin-right:6px}.iti--allow-dropdown input,.iti--allow-dropdown input[type=text],.iti--allow-dropdown input[type=tel],.iti--separate-dial-code input,.iti--separate-dial-code input[type=text],.iti--separate-dial-code input[type=tel]{padding-right:6px;padding-left:52px;margin-left:0}.iti--allow-dropdown .iti__flag-container,.iti--separate-dial-code .iti__flag-container{right:auto;left:0}.iti--allow-dropdown .iti__flag-container:hover{cursor:pointer}.iti--allow-dropdown input[disabled]+.iti__flag-container:hover,.iti--allow-dropdown input[readonly]+.iti__flag-container:hover{cursor:default}.iti--allow-dropdown input[disabled]+.iti__flag-container:hover .iti__selected-flag,.iti--allow-dropdown input[readonly]+.iti__flag-container:hover .iti__selected-flag{background-color:transparent}.iti--separate-dial-code .iti__selected-dial-code{margin-left:6px}.iti--container{position:absolute;top:-1000px;left:-1000px;z-index:1060;padding:1px}.iti--container:hover{cursor:pointer}.iti-mobile .iti--container{top:30px;bottom:30px;left:30px;right:30px;position:fixed}.iti-mobile .iti__country-list{max-height:100%;width:100%}.iti-mobile .iti__country{padding:10px;line-height:1.5em}.iti__flag{width:20px}.iti__flag.iti__be{width:18px}.iti__flag.iti__ch{width:15px}.iti__flag.iti__mc{width:19px}.iti__flag.iti__ne{width:18px}.iti__flag.iti__np{width:13px}.iti__flag.iti__va{width:15px}@media (-webkit-min-device-pixel-ratio: 2),(min-resolution: 192dpi){.iti__flag{background-size:5652px 15px}}.iti__flag.iti__ac{height:10px;background-position:0px 0px}.iti__flag.iti__ad{height:14px;background-position:-22px 0px}.iti__flag.iti__ae{height:10px;background-position:-44px 0px}.iti__flag.iti__af{height:14px;background-position:-66px 0px}.iti__flag.iti__ag{height:14px;background-position:-88px 0px}.iti__flag.iti__ai{height:10px;background-position:-110px 0px}.iti__flag.iti__al{height:15px;background-position:-132px 0px}.iti__flag.iti__am{height:10px;background-position:-154px 0px}.iti__flag.iti__ao{height:14px;background-position:-176px 0px}.iti__flag.iti__aq{height:14px;background-position:-198px 0px}.iti__flag.iti__ar{height:13px;background-position:-220px 0px}.iti__flag.iti__as{height:10px;background-position:-242px 0px}.iti__flag.iti__at{height:14px;background-position:-264px 0px}.iti__flag.iti__au{height:10px;background-position:-286px 0px}.iti__flag.iti__aw{height:14px;background-position:-308px 0px}.iti__flag.iti__ax{height:13px;background-position:-330px 0px}.iti__flag.iti__az{height:10px;background-position:-352px 0px}.iti__flag.iti__ba{height:10px;background-position:-374px 0px}.iti__flag.iti__bb{height:14px;background-position:-396px 0px}.iti__flag.iti__bd{height:12px;background-position:-418px 0px}.iti__flag.iti__be{height:15px;background-position:-440px 0px}.iti__flag.iti__bf{height:14px;background-position:-460px 0px}.iti__flag.iti__bg{height:12px;background-position:-482px 0px}.iti__flag.iti__bh{height:12px;background-position:-504px 0px}.iti__flag.iti__bi{height:12px;background-position:-526px 0px}.iti__flag.iti__bj{height:14px;background-position:-548px 0px}.iti__flag.iti__bl{height:14px;background-position:-570px 0px}.iti__flag.iti__bm{height:10px;background-position:-592px 0px}.iti__flag.iti__bn{height:10px;background-position:-614px 0px}.iti__flag.iti__bo{height:14px;background-position:-636px 0px}.iti__flag.iti__bq{height:14px;background-position:-658px 0px}.iti__flag.iti__br{height:14px;background-position:-680px 0px}.iti__flag.iti__bs{height:10px;background-position:-702px 0px}.iti__flag.iti__bt{height:14px;background-position:-724px 0px}.iti__flag.iti__bv{height:15px;background-position:-746px 0px}.iti__flag.iti__bw{height:14px;background-position:-768px 0px}.iti__flag.iti__by{height:10px;background-position:-790px 0px}.iti__flag.iti__bz{height:14px;background-position:-812px 0px}.iti__flag.iti__ca{height:10px;background-position:-834px 0px}.iti__flag.iti__cc{height:10px;background-position:-856px 0px}.iti__flag.iti__cd{height:15px;background-position:-878px 0px}.iti__flag.iti__cf{height:14px;background-position:-900px 0px}.iti__flag.iti__cg{height:14px;background-position:-922px 0px}.iti__flag.iti__ch{height:15px;background-position:-944px 0px}.iti__flag.iti__ci{height:14px;background-position:-961px 0px}.iti__flag.iti__ck{height:10px;background-position:-983px 0px}.iti__flag.iti__cl{height:14px;background-position:-1005px 0px}.iti__flag.iti__cm{height:14px;background-position:-1027px 0px}.iti__flag.iti__cn{height:14px;background-position:-1049px 0px}.iti__flag.iti__co{height:14px;background-position:-1071px 0px}.iti__flag.iti__cp{height:14px;background-position:-1093px 0px}.iti__flag.iti__cr{height:12px;background-position:-1115px 0px}.iti__flag.iti__cu{height:10px;background-position:-1137px 0px}.iti__flag.iti__cv{height:12px;background-position:-1159px 0px}.iti__flag.iti__cw{height:14px;background-position:-1181px 0px}.iti__flag.iti__cx{height:10px;background-position:-1203px 0px}.iti__flag.iti__cy{height:14px;background-position:-1225px 0px}.iti__flag.iti__cz{height:14px;background-position:-1247px 0px}.iti__flag.iti__de{height:12px;background-position:-1269px 0px}.iti__flag.iti__dg{height:10px;background-position:-1291px 0px}.iti__flag.iti__dj{height:14px;background-position:-1313px 0px}.iti__flag.iti__dk{height:15px;background-position:-1335px 0px}.iti__flag.iti__dm{height:10px;background-position:-1357px 0px}.iti__flag.iti__do{height:14px;background-position:-1379px 0px}.iti__flag.iti__dz{height:14px;background-position:-1401px 0px}.iti__flag.iti__ea{height:14px;background-position:-1423px 0px}.iti__flag.iti__ec{height:14px;background-position:-1445px 0px}.iti__flag.iti__ee{height:13px;background-position:-1467px 0px}.iti__flag.iti__eg{height:14px;background-position:-1489px 0px}.iti__flag.iti__eh{height:10px;background-position:-1511px 0px}.iti__flag.iti__er{height:10px;background-position:-1533px 0px}.iti__flag.iti__es{height:14px;background-position:-1555px 0px}.iti__flag.iti__et{height:10px;background-position:-1577px 0px}.iti__flag.iti__eu{height:14px;background-position:-1599px 0px}.iti__flag.iti__fi{height:12px;background-position:-1621px 0px}.iti__flag.iti__fj{height:10px;background-position:-1643px 0px}.iti__flag.iti__fk{height:10px;background-position:-1665px 0px}.iti__flag.iti__fm{height:11px;background-position:-1687px 0px}.iti__flag.iti__fo{height:15px;background-position:-1709px 0px}.iti__flag.iti__fr{height:14px;background-position:-1731px 0px}.iti__flag.iti__ga{height:15px;background-position:-1753px 0px}.iti__flag.iti__gb{height:10px;background-position:-1775px 0px}.iti__flag.iti__gd{height:12px;background-position:-1797px 0px}.iti__flag.iti__ge{height:14px;background-position:-1819px 0px}.iti__flag.iti__gf{height:14px;background-position:-1841px 0px}.iti__flag.iti__gg{height:14px;background-position:-1863px 0px}.iti__flag.iti__gh{height:14px;background-position:-1885px 0px}.iti__flag.iti__gi{height:10px;background-position:-1907px 0px}.iti__flag.iti__gl{height:14px;background-position:-1929px 0px}.iti__flag.iti__gm{height:14px;background-position:-1951px 0px}.iti__flag.iti__gn{height:14px;background-position:-1973px 0px}.iti__flag.iti__gp{height:14px;background-position:-1995px 0px}.iti__flag.iti__gq{height:14px;background-position:-2017px 0px}.iti__flag.iti__gr{height:14px;background-position:-2039px 0px}.iti__flag.iti__gs{height:10px;background-position:-2061px 0px}.iti__flag.iti__gt{height:13px;background-position:-2083px 0px}.iti__flag.iti__gu{height:11px;background-position:-2105px 0px}.iti__flag.iti__gw{height:10px;background-position:-2127px 0px}.iti__flag.iti__gy{height:12px;background-position:-2149px 0px}.iti__flag.iti__hk{height:14px;background-position:-2171px 0px}.iti__flag.iti__hm{height:10px;background-position:-2193px 0px}.iti__flag.iti__hn{height:10px;background-position:-2215px 0px}.iti__flag.iti__hr{height:10px;background-position:-2237px 0px}.iti__flag.iti__ht{height:12px;background-position:-2259px 0px}.iti__flag.iti__hu{height:10px;background-position:-2281px 0px}.iti__flag.iti__ic{height:14px;background-position:-2303px 0px}.iti__flag.iti__id{height:14px;background-position:-2325px 0px}.iti__flag.iti__ie{height:10px;background-position:-2347px 0px}.iti__flag.iti__il{height:15px;background-position:-2369px 0px}.iti__flag.iti__im{height:10px;background-position:-2391px 0px}.iti__flag.iti__in{height:14px;background-position:-2413px 0px}.iti__flag.iti__io{height:10px;background-position:-2435px 0px}.iti__flag.iti__iq{height:14px;background-position:-2457px 0px}.iti__flag.iti__ir{height:12px;background-position:-2479px 0px}.iti__flag.iti__is{height:15px;background-position:-2501px 0px}.iti__flag.iti__it{height:14px;background-position:-2523px 0px}.iti__flag.iti__je{height:12px;background-position:-2545px 0px}.iti__flag.iti__jm{height:10px;background-position:-2567px 0px}.iti__flag.iti__jo{height:10px;background-position:-2589px 0px}.iti__flag.iti__jp{height:14px;background-position:-2611px 0px}.iti__flag.iti__ke{height:14px;background-position:-2633px 0px}.iti__flag.iti__kg{height:12px;background-position:-2655px 0px}.iti__flag.iti__kh{height:13px;background-position:-2677px 0px}.iti__flag.iti__ki{height:10px;background-position:-2699px 0px}.iti__flag.iti__km{height:12px;background-position:-2721px 0px}.iti__flag.iti__kn{height:14px;background-position:-2743px 0px}.iti__flag.iti__kp{height:10px;background-position:-2765px 0px}.iti__flag.iti__kr{height:14px;background-position:-2787px 0px}.iti__flag.iti__kw{height:10px;background-position:-2809px 0px}.iti__flag.iti__ky{height:10px;background-position:-2831px 0px}.iti__flag.iti__kz{height:10px;background-position:-2853px 0px}.iti__flag.iti__la{height:14px;background-position:-2875px 0px}.iti__flag.iti__lb{height:14px;background-position:-2897px 0px}.iti__flag.iti__lc{height:10px;background-position:-2919px 0px}.iti__flag.iti__li{height:12px;background-position:-2941px 0px}.iti__flag.iti__lk{height:10px;background-position:-2963px 0px}.iti__flag.iti__lr{height:11px;background-position:-2985px 0px}.iti__flag.iti__ls{height:14px;background-position:-3007px 0px}.iti__flag.iti__lt{height:12px;background-position:-3029px 0px}.iti__flag.iti__lu{height:12px;background-position:-3051px 0px}.iti__flag.iti__lv{height:10px;background-position:-3073px 0px}.iti__flag.iti__ly{height:10px;background-position:-3095px 0px}.iti__flag.iti__ma{height:14px;background-position:-3117px 0px}.iti__flag.iti__mc{height:15px;background-position:-3139px 0px}.iti__flag.iti__md{height:10px;background-position:-3160px 0px}.iti__flag.iti__me{height:10px;background-position:-3182px 0px}.iti__flag.iti__mf{height:14px;background-position:-3204px 0px}.iti__flag.iti__mg{height:14px;background-position:-3226px 0px}.iti__flag.iti__mh{height:11px;background-position:-3248px 0px}.iti__flag.iti__mk{height:10px;background-position:-3270px 0px}.iti__flag.iti__ml{height:14px;background-position:-3292px 0px}.iti__flag.iti__mm{height:14px;background-position:-3314px 0px}.iti__flag.iti__mn{height:10px;background-position:-3336px 0px}.iti__flag.iti__mo{height:14px;background-position:-3358px 0px}.iti__flag.iti__mp{height:10px;background-position:-3380px 0px}.iti__flag.iti__mq{height:14px;background-position:-3402px 0px}.iti__flag.iti__mr{height:14px;background-position:-3424px 0px}.iti__flag.iti__ms{height:10px;background-position:-3446px 0px}.iti__flag.iti__mt{height:14px;background-position:-3468px 0px}.iti__flag.iti__mu{height:14px;background-position:-3490px 0px}.iti__flag.iti__mv{height:14px;background-position:-3512px 0px}.iti__flag.iti__mw{height:14px;background-position:-3534px 0px}.iti__flag.iti__mx{height:12px;background-position:-3556px 0px}.iti__flag.iti__my{height:10px;background-position:-3578px 0px}.iti__flag.iti__mz{height:14px;background-position:-3600px 0px}.iti__flag.iti__na{height:14px;background-position:-3622px 0px}.iti__flag.iti__nc{height:10px;background-position:-3644px 0px}.iti__flag.iti__ne{height:15px;background-position:-3666px 0px}.iti__flag.iti__nf{height:10px;background-position:-3686px 0px}.iti__flag.iti__ng{height:10px;background-position:-3708px 0px}.iti__flag.iti__ni{height:12px;background-position:-3730px 0px}.iti__flag.iti__nl{height:14px;background-position:-3752px 0px}.iti__flag.iti__no{height:15px;background-position:-3774px 0px}.iti__flag.iti__np{height:15px;background-position:-3796px 0px}.iti__flag.iti__nr{height:10px;background-position:-3811px 0px}.iti__flag.iti__nu{height:10px;background-position:-3833px 0px}.iti__flag.iti__nz{height:10px;background-position:-3855px 0px}.iti__flag.iti__om{height:10px;background-position:-3877px 0px}.iti__flag.iti__pa{height:14px;background-position:-3899px 0px}.iti__flag.iti__pe{height:14px;background-position:-3921px 0px}.iti__flag.iti__pf{height:14px;background-position:-3943px 0px}.iti__flag.iti__pg{height:15px;background-position:-3965px 0px}.iti__flag.iti__ph{height:10px;background-position:-3987px 0px}.iti__flag.iti__pk{height:14px;background-position:-4009px 0px}.iti__flag.iti__pl{height:13px;background-position:-4031px 0px}.iti__flag.iti__pm{height:14px;background-position:-4053px 0px}.iti__flag.iti__pn{height:10px;background-position:-4075px 0px}.iti__flag.iti__pr{height:14px;background-position:-4097px 0px}.iti__flag.iti__ps{height:10px;background-position:-4119px 0px}.iti__flag.iti__pt{height:14px;background-position:-4141px 0px}.iti__flag.iti__pw{height:13px;background-position:-4163px 0px}.iti__flag.iti__py{height:11px;background-position:-4185px 0px}.iti__flag.iti__qa{height:8px;background-position:-4207px 0px}.iti__flag.iti__re{height:14px;background-position:-4229px 0px}.iti__flag.iti__ro{height:14px;background-position:-4251px 0px}.iti__flag.iti__rs{height:14px;background-position:-4273px 0px}.iti__flag.iti__ru{height:14px;background-position:-4295px 0px}.iti__flag.iti__rw{height:14px;background-position:-4317px 0px}.iti__flag.iti__sa{height:14px;background-position:-4339px 0px}.iti__flag.iti__sb{height:10px;background-position:-4361px 0px}.iti__flag.iti__sc{height:10px;background-position:-4383px 0px}.iti__flag.iti__sd{height:10px;background-position:-4405px 0px}.iti__flag.iti__se{height:13px;background-position:-4427px 0px}.iti__flag.iti__sg{height:14px;background-position:-4449px 0px}.iti__flag.iti__sh{height:10px;background-position:-4471px 0px}.iti__flag.iti__si{height:10px;background-position:-4493px 0px}.iti__flag.iti__sj{height:15px;background-position:-4515px 0px}.iti__flag.iti__sk{height:14px;background-position:-4537px 0px}.iti__flag.iti__sl{height:14px;background-position:-4559px 0px}.iti__flag.iti__sm{height:15px;background-position:-4581px 0px}.iti__flag.iti__sn{height:14px;background-position:-4603px 0px}.iti__flag.iti__so{height:14px;background-position:-4625px 0px}.iti__flag.iti__sr{height:14px;background-position:-4647px 0px}.iti__flag.iti__ss{height:10px;background-position:-4669px 0px}.iti__flag.iti__st{height:10px;background-position:-4691px 0px}.iti__flag.iti__sv{height:12px;background-position:-4713px 0px}.iti__flag.iti__sx{height:14px;background-position:-4735px 0px}.iti__flag.iti__sy{height:14px;background-position:-4757px 0px}.iti__flag.iti__sz{height:14px;background-position:-4779px 0px}.iti__flag.iti__ta{height:10px;background-position:-4801px 0px}.iti__flag.iti__tc{height:10px;background-position:-4823px 0px}.iti__flag.iti__td{height:14px;background-position:-4845px 0px}.iti__flag.iti__tf{height:14px;background-position:-4867px 0px}.iti__flag.iti__tg{height:13px;background-position:-4889px 0px}.iti__flag.iti__th{height:14px;background-position:-4911px 0px}.iti__flag.iti__tj{height:10px;background-position:-4933px 0px}.iti__flag.iti__tk{height:10px;background-position:-4955px 0px}.iti__flag.iti__tl{height:10px;background-position:-4977px 0px}.iti__flag.iti__tm{height:14px;background-position:-4999px 0px}.iti__flag.iti__tn{height:14px;background-position:-5021px 0px}.iti__flag.iti__to{height:10px;background-position:-5043px 0px}.iti__flag.iti__tr{height:14px;background-position:-5065px 0px}.iti__flag.iti__tt{height:12px;background-position:-5087px 0px}.iti__flag.iti__tv{height:10px;background-position:-5109px 0px}.iti__flag.iti__tw{height:14px;background-position:-5131px 0px}.iti__flag.iti__tz{height:14px;background-position:-5153px 0px}.iti__flag.iti__ua{height:14px;background-position:-5175px 0px}.iti__flag.iti__ug{height:14px;background-position:-5197px 0px}.iti__flag.iti__um{height:11px;background-position:-5219px 0px}.iti__flag.iti__un{height:14px;background-position:-5241px 0px}.iti__flag.iti__us{height:11px;background-position:-5263px 0px}.iti__flag.iti__uy{height:14px;background-position:-5285px 0px}.iti__flag.iti__uz{height:10px;background-position:-5307px 0px}.iti__flag.iti__va{height:15px;background-position:-5329px 0px}.iti__flag.iti__vc{height:14px;background-position:-5346px 0px}.iti__flag.iti__ve{height:14px;background-position:-5368px 0px}.iti__flag.iti__vg{height:10px;background-position:-5390px 0px}.iti__flag.iti__vi{height:14px;background-position:-5412px 0px}.iti__flag.iti__vn{height:14px;background-position:-5434px 0px}.iti__flag.iti__vu{height:12px;background-position:-5456px 0px}.iti__flag.iti__wf{height:14px;background-position:-5478px 0px}.iti__flag.iti__ws{height:10px;background-position:-5500px 0px}.iti__flag.iti__xk{height:15px;background-position:-5522px 0px}.iti__flag.iti__ye{height:14px;background-position:-5544px 0px}.iti__flag.iti__yt{height:14px;background-position:-5566px 0px}.iti__flag.iti__za{height:14px;background-position:-5588px 0px}.iti__flag.iti__zm{height:14px;background-position:-5610px 0px}.iti__flag.iti__zw{height:10px;background-position:-5632px 0px}.iti__flag{height:15px;box-shadow:0 0 1px #888;background-image:url(https://cdn.afd.co.uk/plugins/shared/flags.png);background-repeat:no-repeat;background-position:20px 0}@media (-webkit-min-device-pixel-ratio: 2),(min-resolution: 192dpi){.iti__flag{background-image:url(https://cdn.afd.co.uk/plugins/shared/flags@2x.png)}}.iti__flag.iti__np{background-color:transparent}\n"]
            },] }
];
AfdPhoneRegexValidDirective.ctorParameters = () => [
    { type: AfdPceService },
    { type: ElementRef }
];
AfdPhoneRegexValidDirective.propDecorators = {
    country: [{ type: Input }],
    id: [{ type: Input }],
    excludeCountries: [{ type: Input }],
    onlyCountries: [{ type: Input }],
    preferredCountries: [{ type: Input }]
};
export class AfdPhonePceValidDirective {
    constructor(afdPceService, el) {
        this.afdPceService = afdPceService;
        this.el = el;
        // check to see if the regex element exists
        this.hasSyntaxCheck = !!el.nativeElement.hasAttribute('afdPhoneSyntaxValid');
        if (!this.hasSyntaxCheck) {
            console.warn('Warning, you are using the PCE phone validation directive without the regex directive.  ' +
                'This means that you are querying PCE and are being charged for every keypress.  ' +
                'Please add the directive "afdPhoneRegexValid" to the input element');
        }
    }
    ngOnInit() {
        // Set the initial country if there is no Regex directive on the element
        if (!this.hasSyntaxCheck) {
            this.afdPceService.phoneCountry = setInitialCountry(this.country);
        }
    }
    ngAfterViewInit() {
        this.id = typeof this.id === 'undefined' ? 'default' : this.id;
        this.iti = this.afdPceService.iti[this.id];
    }
    validate(control) {
        if (control.value) {
            // Get the phone number
            const { phoneNumber, countryData, numberType, numberTypes } = itiVars(this.iti);
            if (!this.hasSyntaxCheck) {
                // set the country in type AFD service
                this.afdPceService.phoneCountry = setInitialCountry(countryData.iso2);
            }
            // validate
            return this.afdPceService.validatePhone(this.iti.getNumber().replace('+', '')).pipe(map(data => {
                // emit observable update with useful information
                this.afdPceService.phoneInfoSource.next({
                    pceValid: data['body']['Result'] === '1',
                    syntaxValid: this.iti.isValidNumber(),
                    regexValid: this.iti.isValidNumber(),
                    country: this.afdPceService.phoneCountry,
                    isMobile: numberTypes[numberType] === 'MOBILE',
                    isLandLine: numberTypes[numberType] === 'FIXED_LINE',
                    numberType: numberTypes[numberType],
                    phoneNumber: this.iti.getNumber(),
                    phoneNumberE164: this.iti.getNumber(),
                    phoneNumberInternational: this.iti.getNumber(1),
                    phoneNumberNational: this.iti.getNumber(2),
                    phoneNumberRfc3966: this.iti.getNumber(3)
                });
                const output = data;
                output['phone'] = this.iti.getNumber();
                const endEvent = new CustomEvent('afdPhonePceValidationComplete', { detail: output });
                document.dispatchEvent(endEvent);
                // Check if valid
                if (data['body']['Result'] === '1') {
                    return null;
                }
                else {
                    return {
                        afdPhonePceValid: {
                            message: this.afdPceService.settings.messages.phone.invalidPce ? this.afdPceService.settings.messages.phone.invalidPce
                                : data['body']['ErrorText']
                        }
                    };
                }
            }));
        }
        else {
            return new Promise((resolve => resolve(null)));
        }
    }
}
AfdPhonePceValidDirective.decorators = [
    { type: Directive, args: [{
                selector: '[afdPhonePceValid]',
                providers: [
                    {
                        provide: NG_ASYNC_VALIDATORS,
                        useExisting: AfdPhonePceValidDirective,
                        multi: true
                    }
                ]
            },] }
];
AfdPhonePceValidDirective.ctorParameters = () => [
    { type: AfdPceService },
    { type: ElementRef }
];
AfdPhonePceValidDirective.propDecorators = {
    country: [{ type: Input }],
    id: [{ type: Input }],
    excludeCountries: [{ type: Input }],
    onlyCountries: [{ type: Input }],
    preferredCountries: [{ type: Input }]
};
// Can potentially be used for business mapping not currently implemented
export class AfdSearchPhoneDirective {
    constructor(afdPceService) {
        this.afdPceService = afdPceService;
    }
    onAfdPhonePceValidationComplete(e) {
        this.afdPceService.searchPhone(e.detail.phone).subscribe((res) => {
            const endEvent = new CustomEvent('afdPhoneSearchComplete', { detail: res });
            document.dispatchEvent(endEvent);
        });
    }
}
AfdSearchPhoneDirective.decorators = [
    { type: Directive, args: [{
                selector: '[afdSearchPhone]'
            },] }
];
AfdSearchPhoneDirective.ctorParameters = () => [
    { type: AfdPceService }
];
AfdSearchPhoneDirective.propDecorators = {
    onAfdPhonePceValidationComplete: [{ type: HostListener, args: ['document:afdPhonePceValidationComplete', ['$event'],] }]
};
export class AfdPhoneCountriesDirective {
    constructor(el) {
        this.el = el;
        console.log(el);
    }
}
AfdPhoneCountriesDirective.decorators = [
    { type: Directive, args: [{
                selector: '[afdPhoneCountries]'
            },] }
];
AfdPhoneCountriesDirective.ctorParameters = () => [
    { type: ElementRef }
];
function getPhoneObject(phoneNumber, country) {
    if (phoneNumber !== null) {
        // set the default dialing code
        let defaultCountryDialingCode;
        if (typeof country === 'undefined') {
            defaultCountryDialingCode = '+44';
        }
        else {
            defaultCountryDialingCode = '+' + Phone.getCountryCodeForRegionCode(country);
        }
        // If no dialing code is added to the front of the number, then add the default one
        if (phoneNumber.substr(0, 1) !== '+' && phoneNumber.substr(0, 2) !== '00') {
            phoneNumber = defaultCountryDialingCode + phoneNumber;
        }
        // create the Awesome phone object
        return new Phone(phoneNumber);
    }
    else {
        return null;
    }
}
function setInitialCountry(countryAttribute) {
    // default to GB if not set
    const initialCountry = typeof countryAttribute !== 'undefined' ? countryAttribute : 'GB';
    return setCountry(initialCountry);
}
function setCountry(newCountry) {
    let phoneCountry = {
        name: null,
        iso2: null,
        iso3: null
    };
    if (typeof newCountry !== 'undefined') {
        for (const country of Countries) {
            if (country.iso2 === newCountry.toUpperCase()) {
                phoneCountry = country;
            }
        }
    }
    return phoneCountry;
}
function itiSettings(country, excludeCountries, onlyCountries, preferredCountries) {
    const settings = {
        utilsScript: 'https://cdn.afd.co.uk/plugins/shared/utils.js',
        separateDialCode: true,
        initialCountry: country
    };
    if (typeof excludeCountries !== 'undefined') {
        settings['excludeCountries'] = excludeCountries;
    }
    if (typeof onlyCountries !== 'undefined') {
        settings['onlyCountries'] = onlyCountries;
    }
    if (typeof preferredCountries !== 'undefined') {
        settings['preferredCountries'] = preferredCountries;
    }
    return settings;
}
function itiVars(iti) {
    return {
        phoneNumber: iti.getNumber(),
        countryData: iti.getSelectedCountryData(),
        numberType: iti.getNumberType(),
        numberTypes: [
            'FIXED_LINE',
            'MOBILE',
            'FIXED_LINE_OR_MOBILE',
            'TOLL_FREE',
            'PREMIUM_RATE',
            'SHARED_COST',
            'VOIP',
            'PERSONAL_NUMBER',
            'PAGER',
            'UAN',
            'VOICEMAIL'
        ]
    };
}
//# sourceMappingURL=data:application/json;base64, 