/*!
 * KoliBri - The accessible HTML-Standard
 */
'use strict';

var index = require('./index-DBbl8tEq.js');
var tslib_es6 = require('./tslib.es6-Cm0ytgPY.js');
var component = require('./component-C1vBQDN-.js');
var componentNames = require('./component-names-5KS_pYRF.js');
var common = require('./common-DAAt5YQ7.js');
var FormFieldStateWrapper = require('./FormFieldStateWrapper-C93KLp60.js');
var clsx = require('./clsx-Bm_HQUnh.js');
var component$1 = require('./component-4yrnT9ww.js');
var _Uint8Array = require('./_Uint8Array-BAQUGozM.js');
var icons = require('./icons-D80xUBDS.js');

const KolButtonFc = (props) => {
    const { label, icons, hideLabel, disabled, onClick } = props, other = tslib_es6.__rest(props, ["label", "icons", "hideLabel", "disabled", "onClick"]);
    return index.h(componentNames.KolButtonWcTag, Object.assign({ _label: label, _disabled: disabled, _icons: icons, _hideLabel: hideLabel, _on: { onClick } }, other));
};

const KolIconButtonFc = (props) => {
    const { componentName = 'button', label, icon, onClick } = props, other = tslib_es6.__rest(props, ["componentName", "label", "icon", "onClick"]);
    const Component = componentName === 'button' ? KolButtonFc : component.IconFC;
    return index.h(Component, Object.assign({ label: label || '', hideLabel: true, icons: `${icon}`, onClick: onClick }, other));
};

const InputAdornment = (_a, children) => {
    var { position = 'start', class: className } = _a, other = tslib_es6.__rest(_a, ["position", "class"]);
    const rootClassName = `kol-input-container__adornment`;
    const positionClassName = `${rootClassName}--${position}`;
    return (index.h("div", Object.assign({ class: clsx.clsx(rootClassName, positionClassName, className) }, other), children));
};

function hasItems(items) {
    if (!items) {
        return false;
    }
    return Array.isArray(items) ? items.length > 0 : Boolean(items);
}
const Container = (_a, children) => {
    var { class: className } = _a, other = tslib_es6.__rest(_a, ["class"]);
    return (index.h("div", Object.assign({ class: clsx.clsx('kol-input-container__container', className) }, other), children));
};
const KolInputContainerFc = (props, children) => {
    const { class: classNames, startAdornment, endAdornment, disabled, msg, touched, containerProps, startAdornmentProps, endAdornmentProps } = props, other = tslib_es6.__rest(props, ["class", "startAdornment", "endAdornment", "disabled", "msg", "touched", "containerProps", "startAdornmentProps", "endAdornmentProps"]);
    const stateCssClasses = {
        ['kol-input-container--disabled']: disabled,
        [`kol-input-container--${FormFieldStateWrapper.getMsgType(msg)}`]: FormFieldStateWrapper.isMsgDefinedAndInputTouched(msg, touched),
    };
    const baseProps = Object.assign({ class: clsx.clsx('kol-input-container', stateCssClasses, classNames) }, other);
    if (!hasItems(startAdornment) && !hasItems(endAdornment)) {
        return (index.h("div", Object.assign({}, baseProps), index.h(Container, Object.assign({}, containerProps), children)));
    }
    return (index.h("div", Object.assign({}, baseProps), index.h(InputAdornment, Object.assign({}, startAdornmentProps, { position: "start" }), startAdornment), index.h(Container, Object.assign({}, containerProps), children), index.h(InputAdornment, Object.assign({}, endAdornmentProps, { position: "end" }), endAdornment)));
};

function getInputContainerProps(state) {
    let icons = undefined;
    let smartButton;
    if ('_icons' in state) {
        icons = state._icons;
    }
    if ('_smartButton' in state) {
        smartButton = state._smartButton;
    }
    return {
        icons,
        smartButton,
        disabled: state._disabled,
        msg: state._msg,
        touched: state._touched,
    };
}
const InputContainerStateWrapperFc = ({ state, startAdornment: defaultStartAdornment, endAdornment: defaultEndAdornment }, children) => {
    var _a, _b;
    const { icons, smartButton, disabled, msg, touched } = getInputContainerProps(state);
    let leftIconProps = icons === null || icons === void 0 ? void 0 : icons.left;
    if (component$1.isString(leftIconProps)) {
        leftIconProps = { icon: leftIconProps };
    }
    let rightIconProps = icons === null || icons === void 0 ? void 0 : icons.right;
    if (component$1.isString(rightIconProps)) {
        rightIconProps = { icon: rightIconProps };
    }
    const startAdornment = [];
    const endAdornment = [];
    if (defaultStartAdornment) {
        if (Array.isArray(defaultStartAdornment)) {
            startAdornment.push(...defaultStartAdornment);
        }
        else {
            startAdornment.push(defaultStartAdornment);
        }
    }
    if (leftIconProps) {
        if (_Uint8Array.isObject(leftIconProps)) {
            startAdornment.push(index.h(component.IconFC, { class: "kol-input-container__icon", icons: leftIconProps.icon, label: (_a = leftIconProps.label) !== null && _a !== void 0 ? _a : '', style: leftIconProps.style }));
        }
        else {
            startAdornment.push(index.h(component.IconFC, { class: "kol-input-container__icon", icons: leftIconProps, label: "" }));
        }
    }
    if (_Uint8Array.isObject(smartButton)) {
        endAdornment.push(index.h(KolIconButtonFc, Object.assign({ componentName: "button", class: "kol-input-container__smart-button" }, smartButton, { hideLabel: true, disabled: disabled })));
    }
    if (rightIconProps) {
        if (_Uint8Array.isObject(rightIconProps)) {
            endAdornment.push(index.h(component.IconFC, { class: "kol-input-container__icon", icons: rightIconProps.icon, label: (_b = rightIconProps.label) !== null && _b !== void 0 ? _b : '', style: rightIconProps.style }));
        }
        else {
            endAdornment.push(index.h(component.IconFC, { class: "kol-input-container__icon", icons: rightIconProps, label: "" }));
        }
    }
    if (defaultEndAdornment) {
        if (Array.isArray(defaultEndAdornment)) {
            endAdornment.push(...defaultEndAdornment);
        }
        else {
            endAdornment.push(defaultEndAdornment);
        }
    }
    return (index.h(KolInputContainerFc, { disabled: disabled, msg: msg, touched: touched, startAdornment: startAdornment, endAdornment: endAdornment }, children));
};

class InputIconController extends FormFieldStateWrapper.InputController {
    constructor(component, name, host) {
        super(component, name, host);
        this.numberStringRegex = /^\d+(\.\d+)?$/;
        this.parseToNumber = (value) => {
            if (typeof value === 'number') {
                return isNaN(value) ? null : value;
            }
            if (this.isNumberString(value)) {
                return parseFloat(value);
            }
            return null;
        };
        this.validateNumber = (propName, value) => {
            return common.watchValidator(this.component, propName, (value) => value === undefined || value === null || typeof value === 'number' || (typeof value === 'string' && this.numberStringRegex.test(value)), new Set(['number', 'NumberString']), value, {
                hooks: {
                    beforePatch: (value, nextState) => {
                        if (nextState === null || nextState === void 0 ? void 0 : nextState.has(propName)) {
                            nextState === null || nextState === void 0 ? void 0 : nextState.set(propName, this.parseToNumber(value));
                        }
                    },
                },
            });
        };
        this.component = component;
    }
    validateIcons(value) {
        icons.validateIcons(this.component, value);
    }
    isNumberString(value) {
        return typeof value === 'string' && this.numberStringRegex.test(value);
    }
    componentWillLoad() {
        super.componentWillLoad();
        this.validateIcons(this.component._icons);
    }
}

exports.InputContainerStateWrapperFc = InputContainerStateWrapperFc;
exports.InputIconController = InputIconController;
exports.KolIconButtonFc = KolIconButtonFc;
//# sourceMappingURL=controller-icon-BfIYzDZs.js.map

//# sourceMappingURL=controller-icon-BfIYzDZs.js.map