import { Host, h } from "@stencil/core";
import { cls } from "../../utils/helpers";
export class XSegment {
    constructor() {
        this.block = false;
        this.compact = false;
        this.align = 'left';
        this.active = '0';
        this.items = [];
    }
    /**
     * Element event on slot make changes
     *
     * @param evt
     */
    handleSlotChange() {
        this.computeChildItems();
    }
    /**
     * On component did loaded hook
     */
    componentDidLoad() {
        this.computeChildItems();
        if (!this.items.length) {
            return;
        }
        setTimeout(() => {
            if (!this.el.querySelectorAll('x-segment-item.active').length) {
                this.selectItem(this.el.querySelectorAll('x-segment-item').item(0));
            }
        }, 10);
    }
    /**
     * Select the segment and set as active
     *
     * @param index
     * @param segment
     */
    selectItem(segment) {
        this.el.querySelectorAll('x-segment-item.active').forEach(item => {
            item.setAttribute('active', 'false');
            item.classList.remove('active');
        });
        setTimeout(() => {
            segment.setAttribute('active', 'true');
            segment.classList.add('active');
            this.active = segment.getAttribute('value');
            this.selected.emit(this.active);
        }, 10);
    }
    /**
     * Update the child tabs and render the result
     */
    computeChildItems() {
        this.items = Array.from(this.el.querySelector('div').children)
            .filter(tab => tab.nodeName.toLowerCase() === 'x-segment-item');
        this.items.forEach((item, index) => {
            if (!item.hasAttribute('value')) {
                item.setAttribute('value', index.toString());
            }
            if (this.active == item.getAttribute('value')) {
                this.selectItem(item);
            }
            item.addEventListener('click', () => {
                this.selectItem(item);
            });
        });
    }
    /**
     * Render element
     */
    render() {
        return h(Host, { key: '7c0cbb6883aac02e1d22c3626bcdd2eb5655b1a0', class: cls(`segment-${this.align}`, {
                'segment-block': this.block,
                'segment-compact': this.compact,
            }) }, h("div", { key: '3cf7fd1e137762ec43d4e0c9ecc924875202b970', class: "segment-container" }, h("slot", { key: 'a90a46fe693fee32fe934ad9154b2396ab3f3836' })));
    }
    static get is() { return "x-segment"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "block": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element block style"
                },
                "attribute": "block",
                "reflect": false,
                "defaultValue": "false"
            },
            "compact": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element compant style"
                },
                "attribute": "compact",
                "reflect": false,
                "defaultValue": "false"
            },
            "align": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "SegmentAlignType",
                    "resolved": "\"between\" | \"center\" | \"evenly\" | \"left\" | \"right\"",
                    "references": {
                        "SegmentAlignType": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-segment/component.tsx",
                            "id": "src/components/x-segment/component.tsx::SegmentAlignType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element align position segments"
                },
                "attribute": "align",
                "reflect": false,
                "defaultValue": "'left'"
            },
            "active": {
                "type": "any",
                "mutable": true,
                "complexType": {
                    "original": "string | number",
                    "resolved": "number | string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element active item key/index"
                },
                "attribute": "active",
                "reflect": true,
                "defaultValue": "'0'"
            }
        };
    }
    static get states() {
        return {
            "items": {}
        };
    }
    static get events() {
        return [{
                "method": "selected",
                "name": "selected",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On select the segment index/key value"
                },
                "complexType": {
                    "original": "string | number",
                    "resolved": "number | string",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "slotchange",
                "method": "handleSlotChange",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
