/*
 * Copyright 2015-2024 Ritense BV, the Netherlands.
 *
 * Licensed under EUPL, Version 1.2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { BehaviorSubject, combineLatest, map } from 'rxjs';
import { CheckboxModule, DropdownModule, InputModule, TagModule, } from 'carbon-components-angular';
import { CARBON_THEME } from '../../models';
import { CommonModule } from '@angular/common';
import { TranslateModule } from '@ngx-translate/core';
import { distinctUntilChanged, filter, take } from 'rxjs/operators';
import { isEqual } from 'lodash';
import { CaseTagsUtils } from '@valtimo/document';
import * as i0 from "@angular/core";
import * as i1 from "@angular/common";
import * as i2 from "carbon-components-angular";
import * as i3 from "@ngx-translate/core";
export class CaseTagsSelectorComponent {
    constructor() {
        this.carbonTheme = CARBON_THEME.WHITE;
        this.selectedCaseTagsChangeEvent = new EventEmitter();
        this._caseTags$ = new BehaviorSubject([]);
        this._selectedCaseTags$ = new BehaviorSubject([]);
        this.listItems$ = combineLatest([
            this._caseTags$,
            this._selectedCaseTags$,
        ]).pipe(filter(([caseTags, selectedCaseTags]) => !!caseTags && !!selectedCaseTags), map(([caseTags, selectedCaseTags]) => caseTags.map(caseTag => ({
            content: caseTag.title,
            selected: !!selectedCaseTags.find(selectedCaseTag => selectedCaseTag.key === caseTag.key),
            key: caseTag.key,
            tagType: caseTag.tagType,
        }))), distinctUntilChanged((previous, current) => isEqual(previous, current)));
    }
    set caseTags(value) {
        this._caseTags$.next((value || []).map(caseTags => ({
            ...caseTags,
            tagType: CaseTagsUtils.getTagTypeFromCaseTagColor(caseTags.color),
        })));
    }
    set selectedCaseTags(value) {
        this._selectedCaseTags$.next(value);
    }
    itemSelected(event) {
        const newSelectedItems = event?.filter(item => item?.selected) || [];
        this._caseTags$.pipe(take(1)).subscribe(caseTags => {
            const newSelectedCaseTags = newSelectedItems
                .map(item => caseTags.find(caseTag => caseTag.key === item.key))
                .filter(caseTag => !!caseTag);
            this.selectedCaseTagsChangeEvent.emit(newSelectedCaseTags);
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: CaseTagsSelectorComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: CaseTagsSelectorComponent, isStandalone: true, selector: "valtimo-case-tags-selector", inputs: { caseTags: "caseTags", selectedCaseTags: "selectedCaseTags", carbonTheme: "carbonTheme", disabled: "disabled" }, outputs: { selectedCaseTagsChangeEvent: "selectedCaseTagsChangeEvent" }, ngImport: i0, template: "<!--\n  ~ Copyright 2015-2024 Ritense BV, the Netherlands.\n  ~\n  ~ Licensed under EUPL, Version 1.2 (the \"License\");\n  ~ you may not use this file except in compliance with the License.\n  ~ You may obtain a copy of the License at\n  ~\n  ~ https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n  ~\n  ~ Unless required by applicable law or agreed to in writing, software\n  ~ distributed under the License is distributed on an \"AS IS\" basis,\n  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  ~ See the License for the specific language governing permissions and\n  ~ limitations under the License.\n  -->\n\n<cds-label class=\"case-tag-selector\" *ngIf=\"listItems$ | async as listItems\">\n  {{ 'dossierManagement.caseTags.searchFieldTitle' | translate }}\n\n  <cds-dropdown\n    [appendInline]=\"false\"\n    type=\"multi\"\n    [disabled]=\"disabled\"\n    selectionFeedback=\"fixed\"\n    (selected)=\"itemSelected($event)\"\n  >\n    <cds-dropdown-list\n      [items]=\"listItems\"\n      [listTpl]=\"colorDropdownItemTemplate\"\n    ></cds-dropdown-list>\n  </cds-dropdown>\n</cds-label>\n\n<ng-template #colorDropdownItemTemplate let-item=\"item\">\n  <div class=\"case-tag-selector-dropdown-template\">\n    <cds-checkbox [checked]=\"item.selected\"></cds-checkbox>\n\n    <cds-tag class=\"cds-tag--no-margin\" [type]=\"item?.tagType\">\n      {{ item?.content }}\n    </cds-tag>\n  </div>\n</ng-template>\n", styles: ["::ng-deep .case-tag-selector .cds--label{width:100%;margin-bottom:0!important}::ng-deep .case-tag-selector .cds--multiselect{margin-top:8px}::ng-deep .case-tag-selector-dropdown-template{display:flex;margin-top:-4px}::ng-deep .case-tag-selector-dropdown-template .cds--tag{margin:0}::ng-deep .case-tag-selector-dropdown-template .generic-span{display:flex;align-items:center}\n/*!\n * Copyright 2015-2024 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "pipe", type: i1.AsyncPipe, name: "async" }, { kind: "ngmodule", type: DropdownModule }, { kind: "component", type: i2.Dropdown, selector: "cds-dropdown, ibm-dropdown", inputs: ["id", "label", "hideLabel", "helperText", "placeholder", "displayValue", "clearText", "size", "type", "theme", "disabled", "skeleton", "inline", "disableArrowKeys", "invalid", "invalidText", "warn", "warnText", "appendInline", "scrollableContainer", "itemValueKey", "selectionFeedback", "menuButtonLabel", "selectedLabel", "dropUp"], outputs: ["selected", "onClose", "close"] }, { kind: "component", type: i2.DropdownList, selector: "cds-dropdown-list, ibm-dropdown-list", inputs: ["ariaLabel", "items", "listTpl", "type", "showTitles"], outputs: ["select", "scroll", "blurIntent"] }, { kind: "ngmodule", type: CheckboxModule }, { kind: "component", type: i2.Checkbox, selector: "cds-checkbox, ibm-checkbox", inputs: ["disabled", "skeleton", "hideLabel", "name", "id", "required", "value", "ariaLabel", "ariaLabelledby", "indeterminate", "checked"], outputs: ["click", "checkedChange", "indeterminateChange"] }, { kind: "ngmodule", type: InputModule }, { kind: "component", type: i2.Label, selector: "cds-label, ibm-label", inputs: ["labelInputID", "disabled", "skeleton", "helperText", "invalidText", "invalid", "warn", "warnText", "ariaLabel"] }, { kind: "ngmodule", type: TranslateModule }, { kind: "pipe", type: i3.TranslatePipe, name: "translate" }, { kind: "ngmodule", type: TagModule }, { kind: "component", type: i2.Tag, selector: "cds-tag, ibm-tag", inputs: ["type", "size", "class"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: CaseTagsSelectorComponent, decorators: [{
            type: Component,
            args: [{ selector: 'valtimo-case-tags-selector', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, imports: [CommonModule, DropdownModule, CheckboxModule, InputModule, TranslateModule, TagModule], template: "<!--\n  ~ Copyright 2015-2024 Ritense BV, the Netherlands.\n  ~\n  ~ Licensed under EUPL, Version 1.2 (the \"License\");\n  ~ you may not use this file except in compliance with the License.\n  ~ You may obtain a copy of the License at\n  ~\n  ~ https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n  ~\n  ~ Unless required by applicable law or agreed to in writing, software\n  ~ distributed under the License is distributed on an \"AS IS\" basis,\n  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  ~ See the License for the specific language governing permissions and\n  ~ limitations under the License.\n  -->\n\n<cds-label class=\"case-tag-selector\" *ngIf=\"listItems$ | async as listItems\">\n  {{ 'dossierManagement.caseTags.searchFieldTitle' | translate }}\n\n  <cds-dropdown\n    [appendInline]=\"false\"\n    type=\"multi\"\n    [disabled]=\"disabled\"\n    selectionFeedback=\"fixed\"\n    (selected)=\"itemSelected($event)\"\n  >\n    <cds-dropdown-list\n      [items]=\"listItems\"\n      [listTpl]=\"colorDropdownItemTemplate\"\n    ></cds-dropdown-list>\n  </cds-dropdown>\n</cds-label>\n\n<ng-template #colorDropdownItemTemplate let-item=\"item\">\n  <div class=\"case-tag-selector-dropdown-template\">\n    <cds-checkbox [checked]=\"item.selected\"></cds-checkbox>\n\n    <cds-tag class=\"cds-tag--no-margin\" [type]=\"item?.tagType\">\n      {{ item?.content }}\n    </cds-tag>\n  </div>\n</ng-template>\n", styles: ["::ng-deep .case-tag-selector .cds--label{width:100%;margin-bottom:0!important}::ng-deep .case-tag-selector .cds--multiselect{margin-top:8px}::ng-deep .case-tag-selector-dropdown-template{display:flex;margin-top:-4px}::ng-deep .case-tag-selector-dropdown-template .cds--tag{margin:0}::ng-deep .case-tag-selector-dropdown-template .generic-span{display:flex;align-items:center}\n/*!\n * Copyright 2015-2024 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n"] }]
        }], propDecorators: { caseTags: [{
                type: Input
            }], selectedCaseTags: [{
                type: Input
            }], carbonTheme: [{
                type: Input
            }], disabled: [{
                type: Input
            }], selectedCaseTagsChangeEvent: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 