'use strict';

var index = require('./index-pX0sBY0-.js');
var domUtils = require('./dom-utils-BdvOgC2b.js');
var tracking = require('./tracking-BPjaTlNR.js');

const radioButtonCss = () => `:host{display:inline-flex;--_ifx-fallback-display:none !important}._ifx-radiobutton-fallback{display:var(--_ifx-fallback-display);position:absolute !important;width:1px !important;height:1px !important;padding:0 !important;margin:-1px !important;overflow:hidden !important;clip:rect(0, 0, 0, 0) !important;border:0 !important}.radioButton__container{box-sizing:border-box;display:inline-flex;flex-direction:row;align-items:center;padding:0px;gap:8px;cursor:pointer;font-family:var(--ifx-font-family);outline:none}.radioButton__container.disabled{cursor:default}.radioButton__container.disabled .radioButton__wrapper{background-color:#BFBBBB;border-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper:hover{background-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper.checked{background-color:#FFFFFF;border-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper.checked::after{background-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper.checked .radioButton__wrapper-mark{background-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper.checked:hover{border-color:#BFBBBB}.radioButton__container.disabled .radioButton__wrapper.checked:hover .radioButton__wrapper-mark{background-color:#BFBBBB}.radioButton__container.disabled .label{color:#BFBBBB}.radioButton__container:not(.disabled) .radioButton__wrapper.error{border-color:#CD002F}.radioButton__container:not(.disabled) .radioButton__wrapper.error:hover,.radioButton__container:not(.disabled) .radioButton__wrapper.error:focus-visible{border-color:#CD002F}.radioButton__container:not(.disabled) .radioButton__wrapper.error.checked::after{border-color:#CD002F;background-color:#CD002F}.radioButton__container:focus:not(.disabled) .radioButton__wrapper::before{content:"";position:absolute;width:calc(100% + 4px);height:calc(100% + 4px);top:50%;left:50%;transform:translate(-50%, -50%);border:2px solid #0A8276;border-radius:50%}.radioButton__container.m .radioButton__wrapper{width:24px;height:24px}.radioButton__container .radioButton__wrapper{width:20px;height:20px;position:relative;display:block;border-radius:50%;background-color:#FFFFFF;border:1px solid #575352}.radioButton__container .radioButton__wrapper .radioButton__wrapper-mark{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);background-color:#0A8276;border-radius:50%;display:flex;justify-content:center;align-items:center;border-color:transparent}.radioButton__container .radioButton__wrapper:hover{background-color:#EEEDED}.radioButton__container .radioButton__wrapper.checked{border-color:#0A8276}.radioButton__container .radioButton__wrapper.checked::after{content:"";position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);width:10px;height:10px;background-color:#0A8276;border-radius:50%}.radioButton__container .radioButton__wrapper.checked:hover{border-color:#08665C}.radioButton__container .radioButton__wrapper.checked:hover .radioButton__wrapper-mark{background-color:#08665C}.radioButton__container .label{display:flex;align-items:center;height:20px;font-style:normal;font-weight:400;font-size:0.875rem;line-height:1.25rem;color:#1D1D1D;flex:none;order:1;flex-grow:0}.radioButton__container .label.label-m{height:24px;font-size:1rem;line-height:1.5rem}`;

const RadioButton = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.ifxChange = index.createEvent(this, "ifxChange", 7);
        this.ifxError = index.createEvent(this, "ifxError", 7);
    }
    get el() { return index.getElement(this); }
    /** If true, the checkbox is disabled and not interactive. */
    disabled = false;
    /** Value submitted when the checkbox is checked. */
    value;
    /** If true, the checkbox is shown in an error state. */
    error = false;
    /** Size of the checkbox (small or medium). */
    size = "s";
    /** Name attribute used when submitting the checkbox in a form. */
    name;
    /** Whether the checkbox is currently checked (can be updated). */
    checked;
    internalChecked = false;
    hasSlot = false;
    /** Returns the current checked state of the checkbox. */
    async isChecked() {
        return this.internalChecked;
    }
    inputElement;
    //private internals: ElementInternals;
    fallbackInput;
    /** Fired when the checked state of the checkbox changes. */
    ifxChange;
    /** Fired when the checkbox enters or leaves an error state. */
    ifxError;
    componentWillLoad() {
        // Fallback for form association
        this.fallbackInput = document.createElement("input");
        this.fallbackInput.type = "radio";
        this.fallbackInput.hidden = true;
        this.fallbackInput.className = "_ifx-radiobutton-fallback";
        this.fallbackInput.style.cssText = `
      display: none !important;
      position: absolute !important;
      opacity: 0 !important;
      pointer-events: none !important;
      width: 0 !important;
      height: 0 !important;
    `;
        this.fallbackInput.setAttribute("aria-hidden", "true");
        this.fallbackInput.tabIndex = -1;
        this.el.appendChild(this.fallbackInput);
        // Initial state
        this.internalChecked = this.checked || false;
        this.hasSlot =
            !!this.el.querySelector("[slot]") || this.el.innerHTML.trim() !== "";
    }
    async componentDidLoad() {
        if (!domUtils.isNestedInIfxComponent(this.el)) {
            const framework = tracking.detectFramework();
            tracking.trackComponent("ifx-radio-button", await framework);
        }
    }
    handleCheckedChange(newValue) {
        this.internalChecked = newValue;
    }
    updateFormValue() {
        // Update both ElementInternals and fallback input
        // if (this.internals?.setFormValue) {
        //   this.internals.setFormValue(this.internalChecked ? this.value : null);
        // }
        this.fallbackInput.checked = this.internalChecked;
        this.fallbackInput.name = this.name;
        this.fallbackInput.value = this.value;
        this.fallbackInput.disabled = this.disabled;
    }
    errorChanged(newValue, oldValue) {
        if (newValue !== oldValue) {
            this.ifxError.emit(newValue);
        }
    }
    handleRadioButtonClick(event) {
        if (this.disabled) {
            event.stopPropagation();
            return;
        }
        this.inputElement.click();
        this.internalChecked = this.inputElement.checked;
        this.checked = this.internalChecked;
        this.ifxChange.emit(this.internalChecked);
        const changeEvent = new CustomEvent("change", {
            bubbles: true,
            composed: true,
            detail: { checked: this.internalChecked },
        });
        this.el.dispatchEvent(changeEvent);
    }
    handleKeyDown(ev) {
        if ([" ", "Enter"].includes(ev.key)) {
            ev.preventDefault();
            this.handleRadioButtonClick(new PointerEvent("click"));
        }
    }
    handleExternalChange(event) {
        const target = event.target;
        if (target === this.el ||
            target.tagName.toLowerCase() !== "ifx-radio-button")
            return;
        if (target.getAttribute("name") === this.name) {
            this.internalChecked = false;
        }
    }
    render() {
        return (index.h("div", { key: 'd64218e469694c2dfb4e62840dcdea1a215a3312', role: "radio", "aria-checked": String(this.internalChecked), "aria-disabled": String(this.disabled), class: `radioButton__container ${this.size} ${this.disabled ? "disabled" : ""}`, onClick: (e) => this.handleRadioButtonClick(e), tabindex: this.disabled ? -1 : 0 }, index.h("div", { key: 'b4177bda89e18d2a7ab95b3ba99b98aaf8f0355e', class: `radioButton__wrapper 
            ${this.internalChecked ? "checked" : ""}  
            ${this.error ? "error" : ""}` }, this.internalChecked && (index.h("div", { key: 'ec6c98b74456c88378a0abd5e78ee734ef17a716', class: "radioButton__wrapper-mark" }))), this.hasSlot && (index.h("div", { key: '76d20887fdff454f2706f5374a83958c9d119f58', class: `label ${this.size === "m" ? "label-m" : ""}` }, index.h("slot", { key: '9775c637065e4ca7a75eba439cda4cf97b953a86' }))), index.h("input", { key: '87534ecb30c2c06b58784c719ae9c2b106859238', type: "radio", hidden: true, ref: (el) => (this.inputElement = el), name: this.name, value: this.value, checked: this.internalChecked, disabled: this.disabled, onClick: (e) => e.stopPropagation() })));
    }
    static get watchers() { return {
        "checked": [{
                "handleCheckedChange": 0
            }],
        "internalChecked": [{
                "updateFormValue": 0
            }],
        "error": [{
                "errorChanged": 0
            }]
    }; }
};
RadioButton.style = radioButtonCss();

exports.ifx_radio_button = RadioButton;
