var _checkAll, _checkbox, _getFormGroup;
import { __classPrivateFieldGet } from "tslib";
import { Injectable } from '@angular/core';
import { FormGroup } from '@angular/forms';
import * as i0 from "@angular/core";
import * as i1 from "./index";
export class ZUICheckboxService {
    constructor() {
        _checkAll.set(this, new Map());
        _checkbox.set(this, new Map());
        _getFormGroup.set(this, (groupName) => { var _a; return (_a = __classPrivateFieldGet(this, _checkbox)) === null || _a === void 0 ? void 0 : _a.get(groupName); });
        this.addControl = (value) => {
            const { name, groupName, formControl } = value;
            const groupCheckbox = __classPrivateFieldGet(this, _checkbox).get(groupName);
            if (groupCheckbox) {
                groupCheckbox.addControl(name, formControl);
            }
            else {
                __classPrivateFieldGet(this, _checkbox).set(groupName, new FormGroup({ [name]: formControl }));
            }
        };
        this.addControlCheckAll = (component) => {
            __classPrivateFieldGet(this, _checkAll).set(component.groupName, component);
        };
        this.removeControl = (groupName, name) => {
            const formGroup = __classPrivateFieldGet(this, _getFormGroup).call(this, groupName);
            formGroup.removeControl(name);
        };
        this.change = (input) => {
            const { name, groupName, value } = input;
            const formGroup = __classPrivateFieldGet(this, _getFormGroup).call(this, groupName);
            if (formGroup) {
                const arrayValue = Object.values(formGroup.value);
                const isCheckAll = arrayValue.every(Boolean);
                const isExistCheck = arrayValue.some(Boolean);
                const checkAllComponent = __classPrivateFieldGet(this, _checkAll).get(groupName);
                formGroup.patchValue({ [name]: value });
                if (checkAllComponent) {
                    checkAllComponent.indeterminate = false;
                    checkAllComponent.formControl.setValue(false);
                    if (isCheckAll) {
                        checkAllComponent.formControl.setValue(true);
                    }
                    else if (isExistCheck) {
                        setTimeout(() => (checkAllComponent.indeterminate = true));
                    }
                }
                // console.log('value', value, arrayValue)
                // console.log('formGroup', formGroup.value)
            }
        };
        this.changeAll = (groupName, value) => {
            const formGroup = __classPrivateFieldGet(this, _getFormGroup).call(this, groupName);
            formGroup.setValue(Object.keys(formGroup.value).reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item]: value })), {}));
        };
    }
}
_checkAll = new WeakMap(), _checkbox = new WeakMap(), _getFormGroup = new WeakMap();
ZUICheckboxService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ZUICheckboxService_Factory() { return new i1.ZUICheckboxService(); }, token: i1.ZUICheckboxService, providedIn: "root" });
ZUICheckboxService.decorators = [
    { type: Injectable, args: [{ providedIn: 'root' },] }
];
//# sourceMappingURL=index.service.js.map