import * as i0 from '@angular/core';
import { Component, Input, EventEmitter, forwardRef, Output, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';

class SelectButtonItemComponent {
    label;
    active = false;
    first = false;
    last = false;
    disabled = false;
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonItemComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.14", type: SelectButtonItemComponent, selector: "s-select-button-item", inputs: { label: "label", active: "active", first: "first", last: "last", disabled: "disabled" }, ngImport: i0, template: "<button\n    class=\"select-button-item\"\n    [ngClass]=\"{\n        'select-button-item--active': active,\n        'select-button-item--disabled': disabled,\n        'select-button-item--first': first,\n        'select-button-item--last': last\n    }\">\n    <span class=\"select-button-item__label\">{{ label }}</span>\n</button>", styles: [".select-button-item{padding:8px 12px;cursor:pointer;background-color:#fff;border:none;border-left:1px solid #c1c1cc;border-top:1px solid #c1c1cc;border-bottom:1px solid #c1c1cc}.select-button-item__label{font-family:\"Open Sans\" sans-serif;font-size:14px}.select-button-item:not(.select-button-item--disabled):not(.select-button-item--active):hover{background-color:#eeebf2}.select-button-item--active,.select-button-item--active:hover{background-color:#c1c1cc;font-weight:700}.select-button-item--disabled{opacity:.5;cursor:default}.select-button-item--first{border-top-left-radius:2px;border-bottom-left-radius:2px}.select-button-item--last{border-right:1px solid #c1c1cc;border-top-right-radius:2px;border-bottom-right-radius:2px}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonItemComponent, decorators: [{
            type: Component,
            args: [{ selector: "s-select-button-item", template: "<button\n    class=\"select-button-item\"\n    [ngClass]=\"{\n        'select-button-item--active': active,\n        'select-button-item--disabled': disabled,\n        'select-button-item--first': first,\n        'select-button-item--last': last\n    }\">\n    <span class=\"select-button-item__label\">{{ label }}</span>\n</button>", styles: [".select-button-item{padding:8px 12px;cursor:pointer;background-color:#fff;border:none;border-left:1px solid #c1c1cc;border-top:1px solid #c1c1cc;border-bottom:1px solid #c1c1cc}.select-button-item__label{font-family:\"Open Sans\" sans-serif;font-size:14px}.select-button-item:not(.select-button-item--disabled):not(.select-button-item--active):hover{background-color:#eeebf2}.select-button-item--active,.select-button-item--active:hover{background-color:#c1c1cc;font-weight:700}.select-button-item--disabled{opacity:.5;cursor:default}.select-button-item--first{border-top-left-radius:2px;border-bottom-left-radius:2px}.select-button-item--last{border-right:1px solid #c1c1cc;border-top-right-radius:2px;border-bottom-right-radius:2px}\n"] }]
        }], propDecorators: { label: [{
                type: Input,
                args: [{ required: true }]
            }], active: [{
                type: Input
            }], first: [{
                type: Input
            }], last: [{
                type: Input
            }], disabled: [{
                type: Input
            }] } });

class SelectButtonComponent {
    items;
    multiple = false;
    itemSelected = new EventEmitter();
    itemClicked = new EventEmitter();
    disabled = false;
    activeItems = new Set();
    onChange;
    onTouched;
    writeValue(value) {
        if (!value) {
            return;
        }
        this.activeItems.clear();
        if (Array.isArray(value)) {
            value.forEach((item) => {
                this.items.forEach((iItem) => {
                    if (this._compareItems(item, iItem)) {
                        this.activeItems.add(iItem);
                    }
                });
            });
        }
        else {
            this.items.forEach((iItem) => {
                if (this._compareItems(value, iItem)) {
                    this.activeItems.add(iItem);
                }
            });
        }
    }
    registerOnChange(onChange) {
        this.onChange = onChange;
    }
    registerOnTouched(onTouched) {
        this.onTouched = onTouched;
    }
    setDisabledState(disabled) {
        this.disabled = disabled;
    }
    onItemSelect(item) {
        if (this.disabled || item.disabled) {
            return;
        }
        this.itemClicked.emit(item);
        if (!this.multiple) {
            this.activeItems.clear();
        }
        this.activeItems.add(item);
        this.itemSelected.emit([...this.activeItems]);
        this.onChange?.([...this.activeItems]);
        this.onTouched?.([...this.activeItems]);
    }
    _compareItems(item1, item2) {
        const _compare = (a, b) => {
            if (a === b) {
                return true;
            }
            if (a === undefined ||
                b === undefined ||
                typeof a !== 'object' ||
                a === null ||
                typeof b !== 'object' ||
                b === null) {
                return false;
            }
            if (Array.isArray(a) !== Array.isArray(b)) {
                return false;
            }
            let keysA = Object.keys(a);
            let keysB = Object.keys(b);
            if (keysA.length !== keysB.length) {
                return false;
            }
            for (let key of keysA) {
                if (!keysB.includes(key) || !_compare(a[key], b[key])) {
                    return false;
                }
            }
            for (let key in a) {
                if (a.hasOwnProperty(key) !== b.hasOwnProperty(key)) {
                    return false;
                }
            }
            return true;
        };
        return _compare(item1, item2);
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.14", type: SelectButtonComponent, selector: "s-select-button", inputs: { items: "items", multiple: "multiple" }, outputs: { itemSelected: "itemSelected", itemClicked: "itemClicked" }, providers: [
            {
                provide: NG_VALUE_ACCESSOR,
                useExisting: forwardRef(() => SelectButtonComponent),
                multi: true,
            },
        ], ngImport: i0, template: "<div\n    class=\"select-button\"\n    [ngClass]=\"{ 'select-button--disabled': disabled }\">\n    @for(item of items; track item.value; let i = $index) {\n        <s-select-button-item\n            *ngFor=\"let item of items; index as i\"\n            [label]=\"item.label\"\n            [active]=\"activeItems.has(item)\"\n            [disabled]=\"disabled || (item.disabled ?? false)\"\n            [first]=\"i === 0\"\n            [last]=\"i === items.length - 1\"\n            (click)=\"onItemSelect(item)\">\n        </s-select-button-item>\n    }\n</div>", styles: [".select-button{overflow:hidden}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "component", type: SelectButtonItemComponent, selector: "s-select-button-item", inputs: ["label", "active", "first", "last", "disabled"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonComponent, decorators: [{
            type: Component,
            args: [{ selector: 's-select-button', providers: [
                        {
                            provide: NG_VALUE_ACCESSOR,
                            useExisting: forwardRef(() => SelectButtonComponent),
                            multi: true,
                        },
                    ], template: "<div\n    class=\"select-button\"\n    [ngClass]=\"{ 'select-button--disabled': disabled }\">\n    @for(item of items; track item.value; let i = $index) {\n        <s-select-button-item\n            *ngFor=\"let item of items; index as i\"\n            [label]=\"item.label\"\n            [active]=\"activeItems.has(item)\"\n            [disabled]=\"disabled || (item.disabled ?? false)\"\n            [first]=\"i === 0\"\n            [last]=\"i === items.length - 1\"\n            (click)=\"onItemSelect(item)\">\n        </s-select-button-item>\n    }\n</div>", styles: [".select-button{overflow:hidden}\n"] }]
        }], propDecorators: { items: [{
                type: Input,
                args: [{ required: true }]
            }], multiple: [{
                type: Input
            }], itemSelected: [{
                type: Output
            }], itemClicked: [{
                type: Output
            }] } });

class SelectButtonModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonModule, declarations: [SelectButtonComponent,
            SelectButtonItemComponent], imports: [CommonModule], exports: [SelectButtonComponent] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonModule, imports: [CommonModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: SelectButtonModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    declarations: [
                        SelectButtonComponent,
                        SelectButtonItemComponent,
                    ],
                    exports: [SelectButtonComponent],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { SelectButtonComponent, SelectButtonModule };
//# sourceMappingURL=seniorsistemas-angular-components-select-button.mjs.map
