import { randomString } from "@verdocs/js-sdk";
import { Host, h } from "@stencil/core";
const DownIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>`;
const UpIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="m4.5 15.75 7.5-7.5 7.5 7.5" /></svg>`;
/**
 * Display a dropdown that allows multiple options to be selected. Note that events "bubble" from the
 * input field to the container, so you can subscribe to the same DOM events (input, blur, etc) that a
 * normal input would emit.
 *
 * ```ts
 * <verdocs-multiselect label="Methods:" value={[]} options={[...options]} onInput={() => {}} />
 * ```
 */
export class VerdocsMultiselect {
    constructor() {
        /**
         * The label for the field.
         */
        this.label = '';
        /**
         * The placeholder for the input element when no options are selected.
         */
        this.placeholder = 'Select...';
        /**
         * The currently selected options.
         */
        this.selectedOptions = [];
        this.showPicker = false;
    }
    handleToggleOption(e, option) {
        var _a;
        if (e.target.checked) {
            this.selectedOptions = [...this.selectedOptions, option.value];
        }
        else {
            this.selectedOptions = this.selectedOptions.filter(selected => selected !== option.value);
        }
        (_a = this.selectionChanged) === null || _a === void 0 ? void 0 : _a.emit({ selectedOptions: this.selectedOptions });
    }
    render() {
        const elId = `verdocs-dropdown-${randomString(8)}`;
        return (h(Host, { key: 'eafa54e1dae78058303d3b80391745f85cd3eae1', class: this.showPicker ? 'open' : 'closed' }, h("label", { key: 'a584b5df194714f0ac33cba4c249f5360ba61985' }, this.label ? h("div", { class: "input-label" }, this.label + ':') : h("div", null), h("div", { key: 'ba8d22afa9a3b561906fc5866c7644ef8c6c9054', class: "input-wrapper" }, h("button", { key: '517b669dc1a5a95cae442f316678068862e287b2', id: elId, class: "trigger", value: "", onClick: () => (this.showPicker = !this.showPicker) }, this.selectedOptions.length === 0 ? (h("span", { class: "placeholder" }, this.placeholder)) : (this.selectedOptions.map(option => { var _a; return h("span", { class: "selected-option" }, ((_a = this.options.find(opt => opt.value === option)) === null || _a === void 0 ? void 0 : _a.label) || 'Unknown'); }))), h("div", { key: 'aee9c3f227a6637e4589cfb858e9219548c57661', class: "icon", innerHTML: this.showPicker ? UpIcon : DownIcon }), this.showPicker && (h("verdocs-portal", { key: 'be42fed554514dd62147525a96e790a37e8bf67c', anchor: elId, voffset: 2, onClickAway: () => (this.showPicker = false), id: "verdocs-multiselect-menu-items" }, this.options.map((option, i) => (h("div", { class: "option", onClick: () => (this.showPicker = false) }, h("verdocs-checkbox", { size: "small", id: `verdocs-multi-select-opt-${i}`, value: option.value, checked: this.selectedOptions.includes(option.value), onInput: (e) => this.handleToggleOption(e, option) }), h("label", { htmlFor: `verdocs-multi-select-option-${i}` }, option.label))))))))));
    }
    static get is() { return "verdocs-multiselect"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-multiselect.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-multiselect.css"]
        };
    }
    static get properties() {
        return {
            "label": {
                "type": "string",
                "attribute": "label",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The label for the field."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "''"
            },
            "placeholder": {
                "type": "string",
                "attribute": "placeholder",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The placeholder for the input element when no options are selected."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'Select...'"
            },
            "options": {
                "type": "unknown",
                "attribute": "options",
                "mutable": false,
                "complexType": {
                    "original": "IMultiSelectOption[]",
                    "resolved": "IMultiSelectOption[]",
                    "references": {
                        "IMultiSelectOption": {
                            "location": "local",
                            "path": "/Users/chad/projects/verdocs/web-sdk/verdocs-web-sdk/src/components/controls/verdocs-multiselect/verdocs-multiselect.tsx",
                            "id": "src/components/controls/verdocs-multiselect/verdocs-multiselect.tsx::IMultiSelectOption"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The options to list."
                },
                "getter": false,
                "setter": false
            },
            "selectedOptions": {
                "type": "unknown",
                "attribute": "selected-options",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The currently selected options."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "[]"
            }
        };
    }
    static get states() {
        return {
            "showPicker": {}
        };
    }
    static get events() {
        return [{
                "method": "selectionChanged",
                "name": "selectionChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "{selectedOptions: string[]}",
                    "resolved": "{ selectedOptions: string[]; }",
                    "references": {}
                }
            }];
    }
}
//# sourceMappingURL=verdocs-multiselect.js.map
