'use strict';

var index = require('./index-p7cvUYAE.js');
var parts = require('./parts-BXshb_S-.js');
var formControlHelpText = require('./form-control-help-text-BdbTxD5g.js');
var formControlErrorText = require('./form-control-error-text-BO58Y6zF.js');
require('./event-types-YOY5TKUN.js');
var utils = require('./utils-CYONRFR6.js');
require('./dinero-BwNN_k7x.js');

const FormControlMonetary = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.formControlInput = index.createEvent(this, "formControlInput", 7);
        this.formControlBlur = index.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 utils.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 parts.inputInvalid;
        }
        if (this.disabled) {
            return parts.inputDisabled;
        }
        if (this.isFocused) {
            return parts.inputFocused;
        }
        return parts.input;
    }
    render() {
        return (index.h(index.Host, { key: '6b13b9d4871571ab80480c044b813f70db17a85b' }, index.h("div", { key: 'de33d4a9dc8c5b5048c8b95d9c860433cb488c7c', class: "form-group d-flex flex-column" }, index.h("label", { key: 'e243e257c45018233a948c880e4fb15676f1b776', part: parts.label, class: "form-label", htmlFor: this.name }, this.label), index.h(formControlHelpText.FormControlHelpText, { key: 'c490aa1db17d5a181666631cd95c509dbb32aece', name: this.name, helpText: this.helpText }), index.h("div", { key: '69dd679301294e3c6a31ce0b880ec291ee6cf23e', class: "input-group" }, index.h("span", { key: '71ed3716a7ea67933950b2140db4552799968193', class: "input-group-text", part: parts.inputAdornment }, "$"), index.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 })), index.h(formControlErrorText.FormControlErrorText, { key: '6bce010df58a187981efcad682cdc54150422bd6', errorText: this.errorText, name: this.name }))));
    }
    static get watchers() { return {
        "defaultValue": ["handleDefaultValueChange"]
    }; }
};

exports.form_control_monetary = FormControlMonetary;
