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_BLOCK_WORKSPACE_CONTEXT } from '../../block-workspace.context-token.js';
import { css, html, customElement, property, state, repeat, nothing } from '../../../../../../external/lit/index.js';
import { UmbTextStyles } from '../../../../../core/style/index.js';
import { UmbContentTypePropertyStructureHelper } from '../../../../../content/content-type/index.js';
import { UmbLitElement } from '../../../../../core/lit-element/index.js';
import { UmbVariantId } from '../../../../../core/variant/index.js';
import './block-workspace-view-edit-property.element.js';
let UmbBlockWorkspaceViewEditPropertiesElement = class UmbBlockWorkspaceViewEditPropertiesElement extends UmbLitElement {
    #managerName;
    #workspaceContext;
    #propertyStructureHelper = new UmbContentTypePropertyStructureHelper(this);
    #properties;
    #visiblePropertiesUniques = [];
    get managerName() {
        return this.#managerName;
    }
    set managerName(value) {
        this.#managerName = value;
        this.#setStructureManager();
    }
    get containerId() {
        return this.#propertyStructureHelper.getContainerId();
    }
    set containerId(value) {
        this.#propertyStructureHelper.setContainerId(value);
    }
    constructor() {
        super();
        this.consumeContext(UMB_BLOCK_WORKSPACE_CONTEXT, (workspaceContext) => {
            this.#workspaceContext = workspaceContext;
            this._ownerEntityType = this.#workspaceContext?.getEntityType();
            this.observe(workspaceContext?.variantId, (variantId) => {
                this._workspaceVariantId = variantId;
                this.#processPropertyStructure();
            }, 'observeVariantId');
            this.#setStructureManager();
        });
    }
    #setStructureManager() {
        if (!this.#workspaceContext || !this.#managerName)
            return;
        this._dataOwner = this.#workspaceContext[this.#managerName];
        const structureManager = this._dataOwner.structure;
        this.#propertyStructureHelper.setStructureManager(structureManager);
        this.observe(this.#propertyStructureHelper.propertyStructure, (properties) => {
            this.#properties = properties;
            this.#processPropertyStructure();
        }, 'observePropertyStructure');
    }
    #processPropertyStructure() {
        if (!this._dataOwner || !this.#properties || !this.#propertyStructureHelper || !this._workspaceVariantId) {
            return;
        }
        const propertyViewGuard = this._dataOwner.propertyViewGuard;
        this.#properties.forEach((property) => {
            const propertyVariantId = new UmbVariantId(property.variesByCulture ? this._workspaceVariantId.culture : null, property.variesBySegment ? this._workspaceVariantId.segment : null);
            this.observe(propertyViewGuard.isPermittedForVariantAndProperty(propertyVariantId, property, this._workspaceVariantId), (permitted) => {
                if (permitted) {
                    this.#visiblePropertiesUniques.push(property.unique);
                    this.#calculateVisibleProperties();
                }
                else {
                    const index = this.#visiblePropertiesUniques.indexOf(property.unique);
                    if (index !== -1) {
                        this.#visiblePropertiesUniques.splice(index, 1);
                        this.#calculateVisibleProperties();
                    }
                }
            }, `propertyViewGuard-permittedForVariantAndProperty-${property.unique}`);
        });
    }
    #calculateVisibleProperties() {
        this._visibleProperties = this.#properties.filter((property) => this.#visiblePropertiesUniques.includes(property.unique));
    }
    render() {
        return this._workspaceVariantId && this._visibleProperties
            ? repeat(this._visibleProperties, (property) => property.alias, (property) => html `<umb-block-workspace-view-edit-property
							class="property"
							.ownerContext=${this._dataOwner}
							.ownerEntityType=${this._ownerEntityType}
							.variantId=${this._workspaceVariantId}
							.property=${property}></umb-block-workspace-view-edit-property>`)
            : nothing;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			.property {
				border-bottom: 1px solid var(--uui-color-divider);
			}
			.property:last-child {
				border-bottom: 0;
			}
		`,
    ]; }
};
__decorate([
    property({ attribute: false })
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "managerName", null);
__decorate([
    property({ type: String, attribute: 'container-name', reflect: false })
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "containerId", null);
__decorate([
    state()
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "_dataOwner", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "_workspaceVariantId", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "_visibleProperties", void 0);
__decorate([
    state()
], UmbBlockWorkspaceViewEditPropertiesElement.prototype, "_ownerEntityType", void 0);
UmbBlockWorkspaceViewEditPropertiesElement = __decorate([
    customElement('umb-block-workspace-view-edit-properties')
], UmbBlockWorkspaceViewEditPropertiesElement);
export { UmbBlockWorkspaceViewEditPropertiesElement };
export default UmbBlockWorkspaceViewEditPropertiesElement;
