/*!
 * Custom elements for Nanopore-Digital Web applications
 */
'use strict';

var renderer = require('./renderer-jVpDRMfV.js');

const renderLabel = ({ label, hasLabelSlot, controlId, labelId, floatLabel, placeholder, alwaysShowPlaceholder, hideLabel, hasValue, rtl, showCharCount, maxlength, charCount, }) => {
    if (!label && !hasLabelSlot)
        return;
    if (floatLabel) {
        return (renderer.h("label", { class: "form-ctrl__float-label", htmlFor: controlId, id: labelId },
            renderer.h("slot", { name: "label" }, label),
            showCharCount && (renderer.h("span", { class: "form-ctrl__label-charcount" }, maxlength ? ` ${charCount}/${maxlength}` : ` ${charCount}`))));
    }
    else {
        return (renderer.h("label", { class: { 'form-ctrl__label': true, 'visually-hide': hideLabel }, htmlFor: controlId, id: labelId },
            renderer.h("div", { class: "form-ctrl__label-wrap" },
                placeholder && alwaysShowPlaceholder && hasValue && rtl && (renderer.h("span", { class: "form-ctrl__label-placeholder" },
                    placeholder,
                    " / ")),
                renderer.h("span", { class: "form-ctrl__labeltext" },
                    renderer.h("slot", { name: "label" }, label)),
                placeholder && alwaysShowPlaceholder && hasValue && !rtl && (renderer.h("span", { class: "form-ctrl__label-placeholder" },
                    " / ",
                    placeholder))),
            showCharCount && (renderer.h("span", { class: "form-ctrl__label-charcount" }, maxlength
                ? maxlength - charCount + ' left'
                : charCount + ' characters'))));
    }
};
const FormControlWrap = (props, children) => {
    const { rtl, floatLabel, label, moreId, helperEndId, type, hasHelperSlot, showInlineError, errorMessage, hasHelperEndSlot, } = props;
    return (renderer.h("div", { class: {
            'form-ctrl': true,
            'has-label': label !== null && !floatLabel,
            'has-float-label': label !== null && floatLabel,
            'has-helper-end': hasHelperEndSlot,
            rtl,
            [props.class]: !!props.class,
        } },
        renderer.h("div", { class: "form-ctrl__wrapper" },
            !floatLabel ? renderLabel({ ...props }) : '',
            renderer.h("div", { class: {
                    'form-ctrl__input': true,
                    'form-ctrl__textarea': type === 'textarea',
                } },
                floatLabel && renderLabel({ ...props, rtl }),
                children),
            (showInlineError || hasHelperSlot) && (renderer.h("div", { class: "form-ctrl__more", id: moreId },
                showInlineError && (renderer.h("div", { class: "form-ctrl__error", "aria-live": "polite" }, errorMessage)),
                renderer.h("div", { class: "form-ctrl__helper" },
                    renderer.h("slot", { name: "helper" }))))),
        hasHelperEndSlot && (renderer.h("div", { class: "form-ctrl__helper-end", id: helperEndId },
            renderer.h("slot", { name: "helper-end" })))));
};
const FormControl = (props, children) => {
    const { ref, onClearText, clearControl, readonly, disabled, control, endSlot, endValueSlot, startSlot, showInlineError, } = props;
    const clickThrough = () => {
        control.setFocus();
    };
    return (renderer.h("div", { class: "form-ctrl__input-wrap", ref: (el) => {
            if (ref)
                ref(el);
        } },
        renderer.h("span", { class: "form-ctrl__slot-start" },
            renderer.h("slot", { name: "start" }),
            startSlot),
        children,
        clearControl && !readonly && !disabled && (renderer.h("button", { type: "button", class: "icon form-ctrl__clear-btn", tabindex: "-1", onTouchStart: onClearText, onMouseDown: onClearText },
            renderer.h("nano-icon", { name: "light/xmark", role: "img", ariaLabel: "Clear content" }))),
        showInlineError && (renderer.h("div", { onClick: clickThrough, class: "form-ctrl__validation-icon" },
            renderer.h("nano-icon", { name: "solid/circle-xmark", class: "form-ctrl__error-icon" }),
            renderer.h("nano-icon", { name: "solid/circle-check", class: "form-ctrl__success-icon" }))),
        renderer.h("span", { class: "form-ctrl__slot-value-end", onClick: clickThrough },
            renderer.h("slot", { name: "value-end" }),
            endValueSlot),
        renderer.h("span", { class: "form-ctrl__slot-end" },
            renderer.h("slot", { name: "end" }),
            endSlot)));
};

exports.FormControl = FormControl;
exports.FormControlWrap = FormControlWrap;
