var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { UMB_USER_GROUP_WORKSPACE_CONTEXT } from '../user-group-workspace.context-token.js';
import { UmbChangeEvent } from '../../../../../core/event/index.js';
import { html, customElement, state, nothing, property } from '../../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { filterFrozenArray } from '../../../../../../libs/observable-api/index.js';
let UmbUserGroupGranularPermissionListElement = class UmbUserGroupGranularPermissionListElement extends UmbLitElement {
    #workspaceContext;
    constructor() {
        super();
        this.consumeContext(UMB_USER_GROUP_WORKSPACE_CONTEXT, (instance) => {
            this.#workspaceContext = instance;
            this.observe(this.#workspaceContext?.data, (userGroup) => {
                this._userGroupPermissions = userGroup?.permissions;
                this._userGroupFallbackPermissions = userGroup?.fallbackPermissions;
            }, 'umbUserGroupGranularPermissionObserver');
        });
    }
    #onValueChange = (e) => {
        e.stopPropagation();
        // TODO: make interface
        const target = e.target;
        const schemaType = target.manifest?.meta.schemaType;
        if (!schemaType)
            throw new Error('Schema type is not available');
        /* Remove all permissions of the same schema type from
        the user group and append the new permissions.
        We do it this way to support appends, updates and deletion without we know the
        exact action but on the changed value */
        const storedValueWithoutSchemaTypeItems = filterFrozenArray(this.#workspaceContext?.getPermissions() || [], (x) => x.$type !== schemaType);
        const permissions = target.permissions || [];
        const newCombinedValue = [...storedValueWithoutSchemaTypeItems, ...permissions];
        this.#workspaceContext?.setPermissions(newCombinedValue);
    };
    render() {
        if (!this._userGroupPermissions)
            return;
        if (!this.entityType) {
            return html `
				<umb-extension-slot
					type="userGranularPermission"
					.filter=${(manifest) => manifest.forEntityTypes === undefined || manifest.forEntityTypes?.length === 0}
					.renderMethod=${this.#renderProperty}></umb-extension-slot>
			`;
        }
        return html `<umb-extension-slot
			type="userGranularPermission"
			.filter=${(manifest) => manifest.forEntityTypes?.includes(this.entityType) || manifest.forEntityTypes?.length === 0}
			.renderMethod=${this.#renderProperty}></umb-extension-slot>`;
    }
    #renderProperty = (extension) => {
        if (!this._userGroupPermissions)
            return nothing;
        if (!extension.component)
            return nothing;
        const manifest = extension.manifest;
        if (!manifest)
            throw new Error('Manifest is not available');
        const label = manifest.meta.labelKey ? this.localize.term(manifest.meta.labelKey) : manifest.meta.label;
        const description = manifest.meta.descriptionKey
            ? this.localize.term(manifest.meta.descriptionKey)
            : manifest.meta.description;
        const schemaType = manifest.meta.schemaType;
        const permissionsForSchemaType = this._userGroupPermissions.filter((permission) => permission.$type === schemaType) || [];
        extension.component.permissions = permissionsForSchemaType;
        extension.component.fallbackPermissions = this._userGroupFallbackPermissions;
        extension.component.addEventListener(UmbChangeEvent.TYPE, this.#onValueChange);
        return html `
			<umb-property-layout .label=${label || ''} .description=${description || ''}>
				<div slot="editor">${extension.component}</div>
			</umb-property-layout>
		`;
    };
};
__decorate([
    property()
], UmbUserGroupGranularPermissionListElement.prototype, "entityType", void 0);
__decorate([
    state()
], UmbUserGroupGranularPermissionListElement.prototype, "_userGroupPermissions", void 0);
__decorate([
    state()
], UmbUserGroupGranularPermissionListElement.prototype, "_userGroupFallbackPermissions", void 0);
UmbUserGroupGranularPermissionListElement = __decorate([
    customElement('umb-user-group-entity-type-granular-permissions')
], UmbUserGroupGranularPermissionListElement);
export { UmbUserGroupGranularPermissionListElement };
export default UmbUserGroupGranularPermissionListElement;
