/*!
 * (C) Ionic http://ionicframework.com - MIT License
 */
'use strict';

var index = require('./index-D6Wc6v08.js');
var ionicGlobal = require('./ionic-global-DuRUel5m.js');
var overlays = require('./overlays-pdcfssMG.js');
var theme = require('./theme-CeDs6Hcv.js');
require('./helpers-DnouLczu.js');
require('./focus-visible-BIj-I3-C.js');
require('./framework-delegate-Bwdk9vFS.js');
require('./gesture-controller-dtqlP_q4.js');

const selectModalIonicCss = ".sc-ion-select-modal-ionic-h{height:100%}.in-modal-default.sc-ion-select-modal-ionic-h ion-content.sc-ion-select-modal-ionic::part(scroll),.in-modal-default.sc-ion-select-modal-ionic-h ion-content.sc-ion-select-modal-ionic [part~=\"scroll\"]{--padding-bottom:var(--token-space-400, var(--token-scale-400, 16px))}ion-item.sc-ion-select-modal-ionic{--border-width:0}ion-item.ion-focused.sc-ion-select-modal-ionic::part(native)::after,ion-item.ion-focused.sc-ion-select-modal-ionic [part~=\"native\"]::after{border:none}ion-toolbar.sc-ion-select-modal-ionic{font-size:var(--token-font-size-450, 18px);font-weight:var(--token-font-weight-medium, 500);letter-spacing:var(--token-font-letter-spacing-0, 0%);line-height:var(--token-font-line-height-700, var(--token-scale-700, 28px));text-decoration:none;text-transform:none}ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic::part(container),ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic [part~=\"container\"]{display:none}ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic::part(label),ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic [part~=\"label\"]{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic::part(label),ion-list.sc-ion-select-modal-ionic ion-radio.sc-ion-select-modal-ionic [part~=\"label\"],ion-list.sc-ion-select-modal-ionic ion-checkbox.sc-ion-select-modal-ionic::part(label),ion-list.sc-ion-select-modal-ionic ion-checkbox.sc-ion-select-modal-ionic [part~=\"label\"]{font-size:var(--token-font-size-400, 16px);font-weight:var(--token-font-weight-medium, 500);letter-spacing:var(--token-font-letter-spacing-0, 0%);line-height:var(--token-font-line-height-600, var(--token-scale-600, 24px));text-decoration:none;text-transform:none}.item-radio-checked.sc-ion-select-modal-ionic,.item-checkbox-checked.sc-ion-select-modal-ionic{--background:var(--token-semantics-primary-100, var(--token-primitives-blue-100, #e9ecfc));--border-radius:var(--token-border-radius-400, var(--token-scale-400, 16px))}ion-content.sc-ion-select-modal-ionic{--padding-start:var(--token-space-400, var(--token-scale-400, 16px)) !important;--padding-end:var(--token-space-400, var(--token-scale-400, 16px)) !important;--padding-bottom:var(--token-space-1200, var(--token-scale-1200, 48px)) !important}ion-content.sc-ion-select-modal-ionic:has(.radio-checked) .ion-focused.sc-ion-select-modal-ionic:not(.item-radio-checked){--background-focused-opacity:1}";

const selectModalIosCss = ".sc-ion-select-modal-ios-h{height:100%}ion-item.sc-ion-select-modal-ios{--inner-padding-end:0}ion-radio.sc-ion-select-modal-ios::after{bottom:0;position:absolute;width:calc(100% - 0.9375rem - 16px);border-width:0px 0px 0.55px 0px;border-style:solid;border-color:var(--ion-item-border-color, var(--ion-border-color, var(--ion-color-step-250, var(--ion-background-color-step-250, #c8c7cc))));content:\"\"}ion-radio.sc-ion-select-modal-ios::after{inset-inline-start:calc(0.9375rem + 16px)}";

const selectModalMdCss = ".sc-ion-select-modal-md-h{height:100%}ion-list.sc-ion-select-modal-md ion-radio.sc-ion-select-modal-md::part(container),ion-list.sc-ion-select-modal-md ion-radio.sc-ion-select-modal-md [part~=\"container\"]{display:none}ion-list.sc-ion-select-modal-md ion-radio.sc-ion-select-modal-md::part(label),ion-list.sc-ion-select-modal-md ion-radio.sc-ion-select-modal-md [part~=\"label\"]{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}ion-item.sc-ion-select-modal-md{--inner-border-width:0}.item-radio-checked.sc-ion-select-modal-md{--background:rgba(var(--ion-color-primary-rgb, 0, 84, 233), 0.08);--background-focused:var(--ion-color-primary, #0054e9);--background-focused-opacity:0.2;--background-hover:var(--ion-color-primary, #0054e9);--background-hover-opacity:0.12}.item-checkbox-checked.sc-ion-select-modal-md{--background-activated:var(--ion-item-color, var(--ion-text-color, #000));--background-focused:var(--ion-item-color, var(--ion-text-color, #000));--background-hover:var(--ion-item-color, var(--ion-text-color, #000));--color:var(--ion-color-primary, #0054e9)}";

const SelectModal = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.options = [];
    }
    closeModal() {
        const modal = this.el.closest('ion-modal');
        if (modal) {
            modal.dismiss();
        }
    }
    findOptionFromEvent(ev) {
        const { options } = this;
        return options.find((o) => o.value === ev.target.value);
    }
    getValues(ev) {
        const { multiple, options } = this;
        if (multiple) {
            // this is a modal with checkboxes (multiple value select)
            // return an array of all the checked values
            return options.filter((o) => o.checked).map((o) => o.value);
        }
        // this is a modal with radio buttons (single value select)
        // return the value that was clicked, otherwise undefined
        const option = ev ? this.findOptionFromEvent(ev) : null;
        return option ? option.value : undefined;
    }
    callOptionHandler(ev) {
        const option = this.findOptionFromEvent(ev);
        const values = this.getValues(ev);
        if (option === null || option === void 0 ? void 0 : option.handler) {
            overlays.safeCall(option.handler, values);
        }
    }
    setChecked(ev) {
        const { multiple } = this;
        const option = this.findOptionFromEvent(ev);
        // this is a modal with checkboxes (multiple value select)
        // we need to set the checked value for this option
        if (multiple && option) {
            option.checked = ev.detail.checked;
        }
    }
    getModalContextClasses() {
        const el = this.el;
        return {
            'in-modal-default': theme.hostContext('ion-modal.modal-default', el),
            'in-modal-sheet': theme.hostContext('ion-modal.modal-sheet', el),
        };
    }
    renderRadioOptions() {
        const checked = this.options.filter((o) => o.checked).map((o) => o.value)[0];
        return (index.h("ion-radio-group", { value: checked, onIonChange: (ev) => this.callOptionHandler(ev) }, this.options.map((option) => (index.h("ion-item", { lines: "none", class: Object.assign({
                // TODO FW-4784
                'item-radio-checked': option.value === checked
            }, theme.getClassMap(option.cssClass)) }, index.h("ion-radio", { value: option.value, disabled: option.disabled, justify: "start", labelPlacement: "end", onClick: () => this.closeModal(), onKeyUp: (ev) => {
                if (ev.key === ' ') {
                    /**
                     * Selecting a radio option with keyboard navigation,
                     * either through the Enter or Space keys, should
                     * dismiss the modal.
                     */
                    this.closeModal();
                }
            } }, option.text))))));
    }
    renderCheckboxOptions() {
        return this.options.map((option) => (index.h("ion-item", { class: Object.assign({
                // TODO FW-4784
                'item-checkbox-checked': option.checked
            }, theme.getClassMap(option.cssClass)) }, index.h("ion-checkbox", { value: option.value, disabled: option.disabled, checked: option.checked, justify: "start", labelPlacement: "end", onIonChange: (ev) => {
                this.setChecked(ev);
                this.callOptionHandler(ev);
                // TODO FW-4784
                index.forceUpdate(this);
            } }, option.text))));
    }
    render() {
        return (index.h(index.Host, { key: '2ceb196accee272559c43225a859befa68027333', class: Object.assign({ [ionicGlobal.getIonMode(this)]: true }, this.getModalContextClasses()) }, index.h("ion-header", { key: 'b0176847bd9ce94e28d72766d6a035a93ff6f10a' }, index.h("ion-toolbar", { key: '49c2b1cf8e82b1d975cca67b154b149823b47b1f' }, this.header !== undefined && index.h("ion-title", { key: 'fdc881fd94c07ca38d60e85170d176c7cf4139fb' }, this.header), index.h("ion-buttons", { key: '8dfc972aa974909ecf2e5782e972b959a900f8c5', slot: "end" }, index.h("ion-button", { key: 'b862896118bd4da0afc17925eab9dc9ed5269220', onClick: () => this.closeModal() }, "Close")))), index.h("ion-content", { key: 'a7f5bf8f88377d978d2c7bdba3abefcc332296a3' }, index.h("ion-list", { key: '23529677979b9ac63e2cba8d55f7f965489b74c5' }, this.multiple === true ? this.renderCheckboxOptions() : this.renderRadioOptions()))));
    }
    get el() { return index.getElement(this); }
};
SelectModal.style = {
    ionic: selectModalIonicCss,
    ios: selectModalIosCss,
    md: selectModalMdCss
};

exports.ion_select_modal = SelectModal;
