import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { b as inputInvalid, c as inputDisabled, d as inputFocused, e as input, l as label, a as inputAdornment } from './parts.js';
import { F as FormControlHelpText } from './form-control-help-text2.js';
import { F as FormControlErrorText } from './form-control-error-text.js';
import './justifi-details2.js';
import './event-types.js';
import './justifi-radio-list-item2.js';
import { b as formatCurrency } from './utils2.js';

const FormControlMonetary = /*@__PURE__*/ proxyCustomElement(class FormControlMonetary extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.formControlInput = createEvent(this, "formControlInput", 7);
        this.formControlBlur = createEvent(this, "formControlBlur", 7);
        this.isFocused = false;
        this.defaultValue = '0';
        this.formatRawValue = (value) => {
            // Remove everything from that is not a number.
            // This will parse out our display formatting as well as prevent users from entering non-numeric characters.
            const formattedValue = value.replace(/\D/g, '');
            return formattedValue;
        };
        this.formatDisplayValue = (value) => {
            return formatCurrency(+value, undefined, true);
        };
        this.updateInput = (newValue) => {
            const name = this.formControl.name;
            const formatRawValue = this.formatRawValue(newValue); // "100000"
            this.formControl.value = this.formatDisplayValue(formatRawValue); // "1,000.00"
            this.inputHandler(name, formatRawValue);
            this.formControlInput.emit({ name: name, value: formatRawValue });
        };
    }
    handleDefaultValueChange(newValue) {
        this.updateInput(newValue);
    }
    componentDidLoad() {
        this.updateInput(this.defaultValue);
    }
    get inputPart() {
        if (this.errorText) {
            return inputInvalid;
        }
        if (this.disabled) {
            return inputDisabled;
        }
        if (this.isFocused) {
            return inputFocused;
        }
        return input;
    }
    render() {
        return (h(Host, { key: '6b13b9d4871571ab80480c044b813f70db17a85b' }, h("div", { key: 'de33d4a9dc8c5b5048c8b95d9c860433cb488c7c', class: "form-group d-flex flex-column" }, h("label", { key: 'e243e257c45018233a948c880e4fb15676f1b776', part: label, class: "form-label", htmlFor: this.name }, this.label), h(FormControlHelpText, { key: 'c490aa1db17d5a181666631cd95c509dbb32aece', name: this.name, helpText: this.helpText }), h("div", { key: '69dd679301294e3c6a31ce0b880ec291ee6cf23e', class: "input-group" }, h("span", { key: '71ed3716a7ea67933950b2140db4552799968193', class: "input-group-text", part: inputAdornment }, "$"), h("input", { key: 'a1ddad4f875b56fde2d1a0054dd2cfe0c3797e0d', ref: el => this.formControl = el, id: this.name, name: this.name, onFocus: () => this.isFocused = true, onBlur: () => {
                this.isFocused = false;
                this.formControlBlur.emit();
            }, onInput: (event) => this.updateInput(event.target.value), onKeyDown: this.keyDownHandler, onPaste: this.keyDownHandler, maxLength: this.maxLength, part: this.inputPart, class: this.errorText ? "form-control is-invalid" : "form-control", type: "text", disabled: this.disabled })), h(FormControlErrorText, { key: '6bce010df58a187981efcad682cdc54150422bd6', errorText: this.errorText, name: this.name }))));
    }
    static get watchers() { return {
        "defaultValue": ["handleDefaultValueChange"]
    }; }
}, [256, "form-control-monetary", {
        "name": [8],
        "label": [1],
        "inputHandler": [16],
        "defaultValue": [1, "default-value"],
        "helpText": [1, "help-text"],
        "errorText": [1, "error-text"],
        "maxLength": [2, "max-length"],
        "keyDownHandler": [16],
        "disabled": [4],
        "isFocused": [32]
    }, undefined, {
        "defaultValue": ["handleDefaultValueChange"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["form-control-monetary"];
    components.forEach(tagName => { switch (tagName) {
        case "form-control-monetary":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, FormControlMonetary);
            }
            break;
    } });
}
defineCustomElement();

export { FormControlMonetary as F, defineCustomElement as d };
