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_WORKSPACE_CONTEXT } from '../../user-workspace.context-token.js';
import { html, customElement, state, nothing, css } from '../../../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../../../../core/lit-element/index.js';
import { UmbTextStyles } from '../../../../../../core/style/index.js';
const elementName = 'umb-user-workspace-assign-access';
let UmbUserWorkspaceAssignAccessElement = class UmbUserWorkspaceAssignAccessElement extends UmbLitElement {
    #workspaceContext;
    constructor() {
        super();
        this._userGroupUniques = [];
        this._documentStartNodeUniques = [];
        this._documentRootAccess = false;
        this._mediaStartNodeUniques = [];
        this._mediaRootAccess = false;
        this.consumeContext(UMB_USER_WORKSPACE_CONTEXT, (instance) => {
            this.#workspaceContext = instance;
            this.observe(this.#workspaceContext?.userGroupUniques, (value) => (this._userGroupUniques = value ?? []), '_observeUserGroupAccess');
            this.observe(this.#workspaceContext?.hasDocumentRootAccess, (value) => (this._documentRootAccess = value ?? false), '_observeDocumentRootAccess');
            this.observe(this.#workspaceContext?.documentStartNodeUniques, (value) => (this._documentStartNodeUniques = value ?? []), '_observeDocumentStartNode');
            this.observe(this.#workspaceContext?.hasMediaRootAccess, (value) => (this._mediaRootAccess = value ?? false), '_observeMediaRootAccess');
            this.observe(this.#workspaceContext?.mediaStartNodeUniques, (value) => (this._mediaStartNodeUniques = value ?? []), '_observeMediaStartNode');
        });
    }
    #onUserGroupsChange(event) {
        event.stopPropagation();
        const target = event.target;
        const selection = target.selection.map((unique) => {
            return { unique };
        });
        // TODO make contexts method
        this.#workspaceContext?.updateProperty('userGroupUniques', selection);
    }
    #onAllowAllDocumentsChange(event) {
        event.stopPropagation();
        const target = event.target;
        // TODO make contexts method
        this.#workspaceContext?.updateProperty('hasDocumentRootAccess', target.checked);
        this.#workspaceContext?.updateProperty('documentStartNodeUniques', []);
    }
    #onDocumentStartNodeChange(event) {
        event.stopPropagation();
        // TODO: get back to this when media have been decoupled from users.
        // The event target is deliberately set to any to avoid an import cycle with media.
        const target = event.target;
        const selection = target.selection.map((unique) => {
            return { unique };
        });
        // TODO make contexts method
        this.#workspaceContext?.updateProperty('documentStartNodeUniques', selection);
        // When specific start nodes are selected, disable root access
        this.#workspaceContext?.updateProperty('hasDocumentRootAccess', false);
    }
    #onAllowAllMediaChange(event) {
        event.stopPropagation();
        const target = event.target;
        // TODO make contexts method
        this.#workspaceContext?.updateProperty('hasMediaRootAccess', target.checked);
        this.#workspaceContext?.updateProperty('mediaStartNodeUniques', []);
    }
    #onMediaStartNodeChange(event) {
        event.stopPropagation();
        // TODO: get back to this when media have been decoupled from users.
        // The event target is deliberately set to any to avoid an import cycle with media.
        const target = event.target;
        const selection = target.selection.map((unique) => {
            return { unique };
        });
        // TODO make contexts method
        this.#workspaceContext?.updateProperty('mediaStartNodeUniques', selection);
        // When specific start nodes are selected, disable root access
        this.#workspaceContext?.updateProperty('hasMediaRootAccess', false);
    }
    render() {
        return html `
			<uui-box>
				<div slot="headline"><umb-localize key="user_assignAccess">Assign Access</umb-localize></div>
				<div id="assign-access">
					${this.#renderGroupAccess()} ${this.#renderDocumentAccess()} ${this.#renderMediaAccess()}
				</div>
			</uui-box>
		`;
    }
    #renderGroupAccess() {
        return html `<umb-property-layout
			label="${this.localize.term('general_groups')}"
			description="${this.localize.term('user_groupsHelp')}">
			<umb-user-group-input
				slot="editor"
				.selection=${this._userGroupUniques.map((reference) => reference.unique)}
				@change=${this.#onUserGroupsChange}></umb-user-group-input>
		</umb-property-layout>`;
    }
    #renderDocumentAccess() {
        return html `
			<umb-property-layout
				label=${this.localize.term('user_startnodes')}
				description=${this.localize.term('user_startnodeshelp')}>
				<div slot="editor">
					<uui-toggle
						style="margin-bottom: var(--uui-size-space-3);"
						label="${this.localize.term('user_allowAccessToAllDocuments')}"
						.checked=${this._documentRootAccess}
						@change=${this.#onAllowAllDocumentsChange}></uui-toggle>
				</div>

				${this._documentRootAccess === false
            ? html `
							<umb-input-document
								slot="editor"
								.selection=${this._documentStartNodeUniques.map((reference) => reference.unique)}
								@change=${this.#onDocumentStartNodeChange}></umb-input-document>
						`
            : nothing}
			</umb-property-layout>
		`;
    }
    #renderMediaAccess() {
        return html `
			<umb-property-layout
				label=${this.localize.term('defaultdialogs_selectMediaStartNode')}
				description=${this.localize.term('user_mediastartnodehelp')}>
				<div slot="editor">
					<uui-toggle
						style="margin-bottom: var(--uui-size-space-3);"
						label="${this.localize.term('user_allowAccessToAllMedia')}"
						.checked=${this._mediaRootAccess}
						@change=${this.#onAllowAllMediaChange}></uui-toggle>
				</div>

				${this._mediaRootAccess === false
            ? html `
							<umb-input-media
								slot="editor"
								folder-filter="foldersOnly"
								.selection=${this._mediaStartNodeUniques.map((reference) => reference.unique)}
								@change=${this.#onMediaStartNodeChange}></umb-input-media>
						`
            : nothing}
			</umb-property-layout>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: block;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbUserWorkspaceAssignAccessElement.prototype, "_userGroupUniques", void 0);
__decorate([
    state()
], UmbUserWorkspaceAssignAccessElement.prototype, "_documentStartNodeUniques", void 0);
__decorate([
    state()
], UmbUserWorkspaceAssignAccessElement.prototype, "_documentRootAccess", void 0);
__decorate([
    state()
], UmbUserWorkspaceAssignAccessElement.prototype, "_mediaStartNodeUniques", void 0);
__decorate([
    state()
], UmbUserWorkspaceAssignAccessElement.prototype, "_mediaRootAccess", void 0);
UmbUserWorkspaceAssignAccessElement = __decorate([
    customElement(elementName)
], UmbUserWorkspaceAssignAccessElement);
export { UmbUserWorkspaceAssignAccessElement };
