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

var tslib_es6 = require('./tslib.es6-Cm0ytgPY.js');
var index = require('./index-DBbl8tEq.js');
var variantQuote = require('./variant-quote-DUWZtljE.js');
var component$1 = require('./component-CYZTV5r0.js');
var controller = require('./controller-DHsc-8t1.js');
require('./common-DAAt5YQ7.js');
var component = require('./component-4yrnT9ww.js');
var FormFieldStateWrapper = require('./FormFieldStateWrapper-C93KLp60.js');
var clsx = require('./clsx-Bm_HQUnh.js');
var dev_utils = require('./dev.utils-Cdn-EtYb.js');

const fieldControlTooltipControllerById = new Map();
const getFieldControlTooltipController = (id) => {
    const tooltipController = fieldControlTooltipControllerById.get(id);
    if (tooltipController) {
        return tooltipController;
    }
    const nextTooltipController = new controller.TooltipController(variantQuote.BaseWebComponent.stateLess);
    nextTooltipController.componentWillLoad({ label: '' });
    fieldControlTooltipControllerById.set(id, nextTooltipController);
    return nextTooltipController;
};
const destroyFieldControlTooltipController = (id) => {
    const tooltipController = fieldControlTooltipControllerById.get(id);
    if (tooltipController) {
        tooltipController.destroy();
        fieldControlTooltipControllerById.delete(id);
    }
};
const InputContainer = (_a, children) => {
    var { class: classNames } = _a, other = tslib_es6.__rest(_a, ["class"]);
    return (index.h("div", Object.assign({ class: clsx.clsx('kol-field-control__input', classNames) }, other), children));
};
const KolFieldControlFc = (props, children) => {
    const { class: classNames, id, disabled, label, hideLabel, labelAlign, renderNoTooltip, hint, renderNoHint, accessKey, shortKey, msg, touched, required, readonly, tooltipAlign, tooltipFloatingRef, fieldControlInputProps, fieldControlLabelProps, fieldControlTooltipProps, fieldControlHintProps } = props, other = tslib_es6.__rest(props, ["class", "id", "disabled", "label", "hideLabel", "labelAlign", "renderNoTooltip", "hint", "renderNoHint", "accessKey", "shortKey", "msg", "touched", "required", "readonly", "tooltipAlign", "tooltipFloatingRef", "fieldControlInputProps", "fieldControlLabelProps", "fieldControlTooltipProps", "fieldControlHintProps"]);
    const canShowHint = !renderNoHint;
    const canShowTooltip = !renderNoTooltip;
    const hasExpertSlot = component.showExpertSlot(label);
    const useTooltipInsteadOfLabel = canShowTooltip && !hasExpertSlot && hideLabel;
    const badgeText = component.buildBadgeTextString(accessKey, shortKey);
    const labelId = dev_utils.createRelatedUniqueId(id, 'label');
    const tooltipController = useTooltipInsteadOfLabel ? getFieldControlTooltipController(id) : undefined;
    if (tooltipController) {
        tooltipController.watchAlign(tooltipAlign);
        tooltipController.watchBadgeText(badgeText || '');
        tooltipController.watchId(labelId);
        tooltipController.watchLabel(label);
    }
    else {
        destroyFieldControlTooltipController(id);
    }
    const forwardedInputRef = fieldControlInputProps === null || fieldControlInputProps === void 0 ? void 0 : fieldControlInputProps.ref;
    const setInputContainerRef = (el) => {
        forwardedInputRef === null || forwardedInputRef === void 0 ? void 0 : forwardedInputRef(el);
        if (tooltipController && el) {
            tooltipController.initContext(el);
            tooltipController.syncListeners(undefined, el, true);
        }
    };
    const components = [
        index.h(index.Fragment, null, index.h(InputContainer, Object.assign({}, fieldControlInputProps, { ref: setInputContainerRef }), children), useTooltipInsteadOfLabel && (index.h("div", { class: clsx.clsx('kol-form-field__tooltip', fieldControlTooltipProps === null || fieldControlTooltipProps === void 0 ? void 0 : fieldControlTooltipProps.class) }, index.h(component$1.TooltipFC, { badgeText: badgeText || '', label: label, align: tooltipAlign, id: labelId, refFloating: tooltipFloatingRef !== null && tooltipFloatingRef !== void 0 ? tooltipFloatingRef : ((el) => {
                tooltipController === null || tooltipController === void 0 ? void 0 : tooltipController.setTooltipElementRef(el);
            }) })))),
        index.h(FormFieldStateWrapper.KolFormFieldLabelFc, Object.assign({}, (fieldControlLabelProps || {}), { id: id, baseClassName: "kol-field-control", class: clsx.clsx(fieldControlLabelProps === null || fieldControlLabelProps === void 0 ? void 0 : fieldControlLabelProps.class, {
                ['kol-field-control__label--visually-hidden']: Boolean(hideLabel),
            }), hasExpertSlot: hasExpertSlot, label: label, accessKey: accessKey, shortKey: shortKey })),
    ];
    if (labelAlign === 'left') {
        components.reverse();
    }
    const stateCssClasses = {
        ['kol-field-control--disabled']: Boolean(disabled),
        ['kol-field-control--required']: Boolean(required),
        ['kol-field-control--touched']: Boolean(touched),
        ['kol-field-control--hide-label']: Boolean(hideLabel),
        ['kol-field-control--read-only']: Boolean(readonly),
        [`kol-field-control--${FormFieldStateWrapper.getMsgType(msg)}`]: Boolean(FormFieldStateWrapper.isMsgDefinedAndInputTouched(msg, touched)),
        [`kol-field-control--label-align-${labelAlign}`]: Boolean(labelAlign),
    };
    return (index.h("div", Object.assign({ class: clsx.clsx('kol-field-control', stateCssClasses, classNames) }, other), components, canShowHint && index.h(FormFieldStateWrapper.KolFormFieldHintFc, Object.assign({}, (fieldControlHintProps || {}), { baseClassName: "kol-field-control", id: id, hint: hint }))));
};

function getFieldControlProps(state) {
    const props = {
        id: state._id,
        disabled: state._disabled,
        msg: state._msg,
        hint: state._hint,
        label: state._label,
        hideLabel: state._hideLabel,
        touched: state._touched,
    };
    if ('_required' in state) {
        props.required = state._required;
    }
    if ('_readOnly' in state) {
        props.readonly = state._readOnly;
    }
    if ('_labelAlign' in state) {
        props.labelAlign = state._labelAlign;
    }
    if ('_accessKey' in state) {
        props.accessKey = state._accessKey;
    }
    if ('_shortKey' in state) {
        props.shortKey = state._shortKey;
    }
    return props;
}
const FieldControlStateWrapper = (_a, children) => {
    var { state } = _a, other = tslib_es6.__rest(_a, ["state"]);
    return (index.h(KolFieldControlFc, Object.assign({}, getFieldControlProps(state), other), children));
};

exports.FieldControlStateWrapper = FieldControlStateWrapper;
//# sourceMappingURL=FieldControlStateWrapper-CElQn7sN.js.map

//# sourceMappingURL=FieldControlStateWrapper-CElQn7sN.js.map