'use strict';

var index = require('./index-p7cvUYAE.js');
var Business = require('./Business-oBifyXkW.js');
var utils = require('./utils-DAyWinG2.js');
var countryConfig = require('./country-config-C1bWmF8P.js');
var index$1 = require('./index-1MT3Z4XI.js');
var parts = require('./parts-BXshb_S-.js');
var formControlErrorText = require('./form-control-error-text-BO58Y6zF.js');
require('./event-types-YOY5TKUN.js');

const FormAddressFields = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.country = Business.CountryCode.USA;
    }
    render() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        const labels = countryConfig.countryLabels[this.country];
        const options = countryConfig.countryOptions[this.country];
        return (index.h(index.Host, { key: '9b18ac26cce34bd3008146f106a260d9cb99de12' }, index.h("div", { key: 'c234c03bc4a3dbe74a0946af78aaed5992c3406c', class: "row gy-3" }, index.h("div", { key: '0ce04acdf48726866bd6beecc25d0008b754984f', class: "col-12" }, index.h("form-control-text", { key: '4d4b5098ebc39e52d0f17c677c4e92980793f46b', name: "line1", label: "Street Address", inputHandler: this.inputHandler, defaultValue: (_a = this.defaultValues) === null || _a === void 0 ? void 0 : _a.line1, errorText: (_b = this.errors) === null || _b === void 0 ? void 0 : _b.line1, helpText: "No PO Boxes." })), index.h("div", { key: '4133b38343c52135f2457bd7a9ad91b094422fb5', class: "col-12" }, index.h("form-control-text", { key: '7e60be69dad198d488030ece4296326bb6368f2c', name: "line2", label: "Address Line 2 (optional)", inputHandler: this.inputHandler, defaultValue: (_c = this.defaultValues) === null || _c === void 0 ? void 0 : _c.line2, errorText: (_d = this.errors) === null || _d === void 0 ? void 0 : _d.line2 })), index.h("div", { key: 'a34a4e57cbd168bbbe065329bcfb393473b0def1', class: "col-12" }, index.h("form-control-text", { key: '46e3005ef99ec988726ffcc37ec73756f002880d', name: "city", label: "City", inputHandler: this.inputHandler, defaultValue: (_e = this.defaultValues) === null || _e === void 0 ? void 0 : _e.city, errorText: (_f = this.errors) === null || _f === void 0 ? void 0 : _f.city })), index.h("div", { key: '2bf3389786351f2338cde13cbf87d28260444174', class: "col-12 col-md-6" }, index.h("form-control-select", { key: '18a2a403d20af45965877601fa8d44a5aea48894', name: "state", label: labels.stateLabel, options: options.stateOptions, inputHandler: this.inputHandler, defaultValue: (_g = this.defaultValues) === null || _g === void 0 ? void 0 : _g.state, errorText: (_h = this.errors) === null || _h === void 0 ? void 0 : _h.state })), index.h("div", { key: '1548ad58c0bd3b96067055601a006fc137ca31a3', class: "col-12 col-md-6" }, index.h("form-control-text", { key: '8e8193efa81f1f5d8c626f76d75d15aba60ac921', name: "postal_code", label: labels.postalLabel, inputHandler: this.inputHandler, defaultValue: (_j = this.defaultValues) === null || _j === void 0 ? void 0 : _j.postal_code, errorText: (_k = this.errors) === null || _k === void 0 ? void 0 : _k.postal_code, maxLength: this.country === Business.CountryCode.USA ? 5 : 7, keyDownHandler: this.country === Business.CountryCode.USA ? utils.numberOnlyHandler : undefined })))));
    }
};

const FormControlNumberMasked = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.formControlInput = index.createEvent(this, "formControlInput", 7);
        this.formControlBlur = index.createEvent(this, "formControlBlur", 7);
        this.imask = null;
        this.isFocused = false;
        this.inputHandler = () => { };
        this.disabled = false;
        this.initializeIMask = () => {
            if (!this.textInput)
                return;
            this.imask = index$1.IMask(this.textInput, {
                mask: this.mask,
            });
            this.imask.on('accept', () => {
                var _a;
                const rawValue = (_a = this.imask) === null || _a === void 0 ? void 0 : _a.unmaskedValue;
                if (rawValue !== undefined) {
                    this.inputHandler(this.name, rawValue);
                }
            });
            this.textInput.addEventListener('blur', () => this.formControlBlur.emit());
        };
        this.handleFormControlInput = (event) => {
            var _a;
            const target = event.target;
            const name = target.getAttribute('name');
            const rawValue = ((_a = this.imask) === null || _a === void 0 ? void 0 : _a.unmaskedValue) || target.value;
            this.formControlInput.emit({ name: name, value: rawValue });
        };
        this.updateInput = (newValue) => {
            if (this.imask && newValue) {
                this.imask.value = String(newValue);
            }
        };
    }
    handleDefaultValueChange(newValue) {
        this.initializeIMask();
        this.updateInput(newValue);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.imask) === null || _a === void 0 ? void 0 : _a.destroy();
    }
    componentDidLoad() {
        if (this.textInput) {
            this.initializeIMask();
            this.updateInput(this.defaultValue);
        }
    }
    get part() {
        if (this.errorText) {
            return parts.inputInvalid;
        }
        if (this.disabled) {
            return parts.inputDisabled;
        }
        if (this.isFocused) {
            return parts.inputFocused;
        }
        return parts.input;
    }
    render() {
        return (index.h(index.Host, { key: '0ff6ce9901d9e804d1b82f9e8830a6d2a4cab67f' }, index.h("div", { key: '1846069b3d0682c1c1059a9a6192610dc6ac7e66', class: "form-group d-flex flex-column" }, index.h("div", { key: '2739a42086660392c2c508c37cdb387303efe592', class: "d-flex align-items-start gap-2" }, index.h("label", { key: 'e2e8149f7284cdfc0548131afa0c0a1d8f2e9385', part: parts.label, class: "form-label", htmlFor: this.name }, this.label), index.h("form-control-tooltip", { key: 'ed0552645fd9929fa10ccd56dae495f27283c168', helpText: this.helpText })), index.h("input", { key: '1f52622e4724ac43f8b430f4742b6f9ed0a5ccf6', ref: el => (this.textInput = el), id: this.name, name: this.name, onFocus: () => (this.isFocused = true), onBlur: () => {
                this.isFocused = false;
                this.formControlBlur.emit();
            }, onInput: this.handleFormControlInput, part: this.part, class: this.errorText ? 'form-control is-invalid' : 'form-control', type: "text", disabled: this.disabled }), index.h(formControlErrorText.FormControlErrorText, { key: '2cce6d31bf2ff20a8c32e1e3d28e1cb8040b192b', errorText: this.errorText, name: this.name }))));
    }
    static get watchers() { return {
        "defaultValue": ["handleDefaultValueChange"]
    }; }
};

const IdentityAddressForm = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.address = {};
        this.inputHandler = (name, value) => {
            this.address[name] = value;
            this.address = Object.assign({}, this.address);
        };
    }
    handleAddressChange(newValues) {
        this.handleFormUpdate(newValues);
    }
    render() {
        return (index.h(index.Host, { key: '71100bc3742c3efaa9c3a948bf3ac5c69281c559' }, index.h("form-address-fields", { key: 'e5e5399686aa272a9cbe45d6914ebeda6a1966f4', country: this.country, errors: this.errors, defaultValues: this.defaultValues, inputHandler: this.inputHandler })));
    }
    static get watchers() { return {
        "address": ["handleAddressChange"]
    }; }
};

exports.form_address_fields = FormAddressFields;
exports.form_control_number_masked = FormControlNumberMasked;
exports.identity_address_form = IdentityAddressForm;
