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

/**
 * @description Componente de lista de checkboxes hierárquica, com suporte a estados
 * pai/filho e indeterminado. Ao marcar/desmarcar o item pai, todos os filhos são
 * afetados, e o estado do pai é calculado automaticamente com base nos filhos.
 * Implementa {@link ControlValueAccessor} para uso em formulários Angular.
 *
 * @example
 * ```html
 * <s-checkbox-list
 *   formControlName="permissoes"
 *   [data]="estruturaPermissoes" />
 * ```
 *
 * @category Inputs
 */
class CheckboxListComponent {
    /**
     * @description Estrutura de dados que define o item raiz e seus filhos.
     * Cada `CheckboxListData` pode conter o label, estado inicial e uma lista de filhos recursiva.
     * Campo obrigatório.
     */
    data;
    state = {
        checked: false,
        indeterminate: false,
        children: {},
    };
    _onChange = () => { };
    _onTouched = () => { };
    writeValue(value) {
        if (value) {
            this.state = value;
        }
        else {
            this.state = {
                checked: false,
                indeterminate: false,
                disabled: false,
                children: this._initializeChildren(this.data),
            };
        }
    }
    get renderType() {
        return this.data.renderType ?? 'inline';
    }
    registerOnChange(onChange) {
        this._onChange = onChange;
    }
    registerOnTouched(onTouched) {
        this._onTouched = onTouched;
    }
    _initializeChildren(item) {
        const childrenState = {};
        if (item.children) {
            item.children.forEach((child) => {
                childrenState[child.label] = {
                    checked: false,
                    indeterminate: false,
                    children: this._initializeChildren(child),
                };
            });
        }
        return childrenState;
    }
    getCheckboxState(item, label) {
        if (item) {
            return item[label];
        }
        else {
            return {};
        }
    }
    updateParent() {
        const _getProperty = (obj, field) => {
            if (obj) {
                return obj[field];
            }
            else {
                return null;
            }
        };
        if (this.data.children && this.data.children.length > 0) {
            const allChecked = this.data.children.every((child) => _getProperty(this.state.children, child.label)?.checked);
            const someChecked = this.data.children.some((child) => _getProperty(this.state.children, child.label)?.checked ||
                _getProperty(this.state.children, child.label)?.indeterminate);
            this.state.checked = allChecked;
            this.state.indeterminate = !allChecked && someChecked;
        }
        this._onChange(this.state);
        this._onTouched();
    }
    onCheckboxChange() {
        if (this.state.disabled) {
            return;
        }
        this.state.checked = !this.state.checked;
        this.state.indeterminate = false;
        this._toggleChildrenCheck(this.data, this.state, this.state.checked);
        this._onChange(this.state);
        this._onTouched();
    }
    _toggleChildrenCheck(item, state, checked) {
        if (item.children?.length) {
            item.children.forEach((child) => {
                if (state.children && !state.children[child.label]) {
                    state.children[child.label] = {
                        checked: false,
                        indeterminate: false,
                        children: this._initializeChildren(child),
                    };
                }
                if (state.children) {
                    state.children[child.label].checked = checked;
                    state.children[child.label].indeterminate = false;
                }
                if (child.children && state.children) {
                    this._toggleChildrenCheck(child, state.children[child.label], checked);
                }
            });
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: CheckboxListComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.14", type: CheckboxListComponent, selector: "s-checkbox-list", inputs: { data: "data" }, providers: [
            {
                provide: NG_VALUE_ACCESSOR,
                useExisting: forwardRef(() => CheckboxListComponent),
                multi: true,
            },
        ], ngImport: i0, template: "<div class=\"checkbox\">\n    <label>\n        <div\n            class=\"checkbox-wrapper\"\n            [class.checkbox-wrapper--disabled]=\"state.disabled\"\n            [class.checkbox-wrapper-inline]=\"renderType === 'inline'\"\n            [class.checkbox-wrapper-block]=\"renderType === 'block'\"\n        >\n            <input\n                type=\"checkbox\"\n                class=\"checkbox-input\"\n                [disabled]=\"state.disabled\"\n                [ngClass]=\"{\n                    'checkbox-input--checked': state.checked,\n                    'checkbox-input--indeterminate': state.indeterminate,\n                }\"\n                (change)=\"onCheckboxChange()\"\n                [attr.aria-checked]=\"state.indeterminate ? 'mixed' : state.checked\"\n                [attr.aria-disabled]=\"state.disabled\"\n            />\n            {{ data.label }}\n        </div>\n    </label>\n    <div\n        *ngIf=\"data.children && data.children.length\"\n        class=\"checkbox-children\"\n    >\n        <s-checkbox-list\n            *ngFor=\"let child of data.children\"\n            [data]=\"child\"\n            [ngModel]=\"getCheckboxState(state.children, child.label)\"\n            (ngModelChange)=\"updateParent()\"\n        >\n        </s-checkbox-list>\n    </div>\n</div>\n", styles: [".checkbox{display:flex;flex-direction:column;gap:10px;margin-bottom:10px}.checkbox .checkbox-wrapper{color:#212533;display:flex;font-family:\"Open Sans\" sans-serif;font-size:14px;gap:12px}.checkbox .checkbox-wrapper-inline{align-items:center}.checkbox .checkbox-wrapper-block{flex-direction:column-reverse}.checkbox .checkbox-wrapper .checkbox-input{appearance:none;background-color:#fff;border:1px solid #c1c1cc;border-radius:4px;cursor:pointer;flex-shrink:0;height:20px;width:20px;position:relative}.checkbox .checkbox-wrapper .checkbox-input--checked{background-color:#428bca;border-color:#428bca}.checkbox .checkbox-wrapper .checkbox-input--checked:after{border:solid white;border-width:0 2px 2px 0;content:\"\";height:12px;left:6px;position:absolute;top:2px;transform:rotate(45deg);width:6px}.checkbox .checkbox-wrapper .checkbox-input--indeterminate{background-color:#428bca;border-color:#428bca}.checkbox .checkbox-wrapper .checkbox-input--indeterminate:after{background-color:#fff;content:\"\";height:2px;left:50%;position:absolute;top:50%;transform:translate(-50%,-50%);width:10px}.checkbox .checkbox-wrapper:not(.checkbox-wrapper--disabled) .checkbox-input:not(.checkbox-input--checked):hover{background-color:#e5eaea;border-color:#ccc}.checkbox .checkbox-wrapper--disabled{opacity:.5;cursor:auto}.checkbox .checkbox-children{margin-left:20px}\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: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: CheckboxListComponent, selector: "s-checkbox-list", inputs: ["data"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: CheckboxListComponent, decorators: [{
            type: Component,
            args: [{ selector: 's-checkbox-list', providers: [
                        {
                            provide: NG_VALUE_ACCESSOR,
                            useExisting: forwardRef(() => CheckboxListComponent),
                            multi: true,
                        },
                    ], template: "<div class=\"checkbox\">\n    <label>\n        <div\n            class=\"checkbox-wrapper\"\n            [class.checkbox-wrapper--disabled]=\"state.disabled\"\n            [class.checkbox-wrapper-inline]=\"renderType === 'inline'\"\n            [class.checkbox-wrapper-block]=\"renderType === 'block'\"\n        >\n            <input\n                type=\"checkbox\"\n                class=\"checkbox-input\"\n                [disabled]=\"state.disabled\"\n                [ngClass]=\"{\n                    'checkbox-input--checked': state.checked,\n                    'checkbox-input--indeterminate': state.indeterminate,\n                }\"\n                (change)=\"onCheckboxChange()\"\n                [attr.aria-checked]=\"state.indeterminate ? 'mixed' : state.checked\"\n                [attr.aria-disabled]=\"state.disabled\"\n            />\n            {{ data.label }}\n        </div>\n    </label>\n    <div\n        *ngIf=\"data.children && data.children.length\"\n        class=\"checkbox-children\"\n    >\n        <s-checkbox-list\n            *ngFor=\"let child of data.children\"\n            [data]=\"child\"\n            [ngModel]=\"getCheckboxState(state.children, child.label)\"\n            (ngModelChange)=\"updateParent()\"\n        >\n        </s-checkbox-list>\n    </div>\n</div>\n", styles: [".checkbox{display:flex;flex-direction:column;gap:10px;margin-bottom:10px}.checkbox .checkbox-wrapper{color:#212533;display:flex;font-family:\"Open Sans\" sans-serif;font-size:14px;gap:12px}.checkbox .checkbox-wrapper-inline{align-items:center}.checkbox .checkbox-wrapper-block{flex-direction:column-reverse}.checkbox .checkbox-wrapper .checkbox-input{appearance:none;background-color:#fff;border:1px solid #c1c1cc;border-radius:4px;cursor:pointer;flex-shrink:0;height:20px;width:20px;position:relative}.checkbox .checkbox-wrapper .checkbox-input--checked{background-color:#428bca;border-color:#428bca}.checkbox .checkbox-wrapper .checkbox-input--checked:after{border:solid white;border-width:0 2px 2px 0;content:\"\";height:12px;left:6px;position:absolute;top:2px;transform:rotate(45deg);width:6px}.checkbox .checkbox-wrapper .checkbox-input--indeterminate{background-color:#428bca;border-color:#428bca}.checkbox .checkbox-wrapper .checkbox-input--indeterminate:after{background-color:#fff;content:\"\";height:2px;left:50%;position:absolute;top:50%;transform:translate(-50%,-50%);width:10px}.checkbox .checkbox-wrapper:not(.checkbox-wrapper--disabled) .checkbox-input:not(.checkbox-input--checked):hover{background-color:#e5eaea;border-color:#ccc}.checkbox .checkbox-wrapper--disabled{opacity:.5;cursor:auto}.checkbox .checkbox-children{margin-left:20px}\n"] }]
        }], propDecorators: { data: [{
                type: Input,
                args: [{ required: true }]
            }] } });

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

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

export { CheckboxListComponent, CheckboxListModule };
//# sourceMappingURL=seniorsistemas-angular-components-checkbox-list.mjs.map
