import { h } from "@stencil/core";
import { trackComponent } from "../../global/utils/tracking";
import { isNestedInIfxComponent } from "../../global/utils/dom-utils";
import { detectFramework } from "../../global/utils/framework-detection";
export class SegmentedControl {
    constructor() {
        this.caption = '';
        this.label = '';
        this.size = 'regular';
        this.required = false;
        this.error = false;
        this.selectedValue = '';
    }
    onSegmentSelect(event) {
        const { previousValue, selectedValue } = this.unselectPreviousSegment(event.detail);
        this.selectedValue = selectedValue;
        this.ifxChange.emit({ previousValue, selectedValue });
    }
    unselectPreviousSegment(newSelectedIndex) {
        let previousValue;
        let selectedValue;
        const segments = this.getSegments();
        segments.forEach((control) => {
            if (control.selected) {
                if (control.segmentIndex !== newSelectedIndex) {
                    control.selected = false;
                    previousValue = control.value;
                }
                else {
                    selectedValue = control.value;
                }
            }
        });
        return { previousValue, selectedValue };
    }
    getSegments() {
        return this.SegmentedControl.querySelectorAll('ifx-segment');
    }
    setActiveSegment() {
        const segments = this.getSegments();
        let activeSegmentedControlFound = false;
        segments.forEach((control, idx) => {
            control.segmentIndex = idx;
            if (activeSegmentedControlFound) {
                if (control.selected)
                    control.selected = false;
            }
            else {
                if (control.selected) {
                    activeSegmentedControlFound = true;
                    this.selectedValue = control.value;
                }
            }
        });
    }
    setSegmentSize() {
        const segments = this.getSegments();
        segments.forEach((control) => {
            control.shadowRoot.querySelector('.segment').classList.add(`segment--${this.size}`);
        });
    }
    async componentDidLoad() {
        if (!isNestedInIfxComponent(this.SegmentedControl)) {
            const framework = detectFramework();
            trackComponent('ifx-segmented-control', await framework);
        }
        this.setActiveSegment();
    }
    render() {
        return (h("div", { key: '74bfd584af640b44d4fdeaa674e0df4003d58383', "aria-value": this.selectedValue, "aria-label": 'segmented control', class: "group" }, h("div", { key: '233e32decc7484857fe34397d10191fa60f101ff', class: 'group__label' }, this.label.trim(), this.required && this.label.trim().toLowerCase() !== "" && h("span", { key: '59e000fd4bf388832d2dc31f2487f90a555aedf7', class: `required ${this.error ? 'error' : ''}` }, "*")), h("div", { key: '4368841e6a763be4407ada54434297b699e8d2b8', class: 'group__controls' }, h("slot", { key: 'f4d60a5f83969042e4198aa67687628b25dbf68b' })), this.caption.trim() &&
            h("div", { key: '4c46eec275fea48fd95df56519739fd2ba97b807', class: `group__caption ${this.error ? 'error' : ''}` }, h("ifx-icon", { key: '656b6becac2b36bb32bf7ba8b4b247fe69e1a212', icon: 'c-info-16' }), " ", this.caption.trim())));
    }
    componentDidRender() {
        this.setSegmentSize();
    }
    static get is() { return "ifx-segmented-control"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["segmented-control.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["segmented-control.css"]
        };
    }
    static get properties() {
        return {
            "caption": {
                "type": "string",
                "attribute": "caption",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "''"
            },
            "label": {
                "type": "string",
                "attribute": "label",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "''"
            },
            "size": {
                "type": "string",
                "attribute": "size",
                "mutable": false,
                "complexType": {
                    "original": "'regular' | 'small'",
                    "resolved": "\"regular\" | \"small\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'regular'"
            },
            "required": {
                "type": "boolean",
                "attribute": "required",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            },
            "error": {
                "type": "boolean",
                "attribute": "error",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get events() {
        return [{
                "method": "ifxChange",
                "name": "ifxChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "{ previousValue: string, selectedValue: string }",
                    "resolved": "{ previousValue: string; selectedValue: string; }",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "SegmentedControl"; }
    static get listeners() {
        return [{
                "name": "segmentSelect",
                "method": "onSegmentSelect",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
//# sourceMappingURL=segmented-control.js.map
