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_CONTENT_WORKSPACE_CONTEXT } from '../constants.js';
import { html, customElement, property, state, nothing } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UMB_VARIANT_CONTEXT, UmbVariantId } from '../../../core/variant/index.js';
import { UmbDataPathPropertyValueQuery } from '../../../core/validation/index.js';
import { UMB_CURRENT_USER_CONTEXT } from '../../../user/current-user/index.js';
let UmbContentWorkspacePropertyElement = class UmbContentWorkspacePropertyElement extends UmbLitElement {
    get alias() {
        return this._alias;
    }
    set alias(value) {
        this._alias = value;
        this.#observePropertyType();
    }
    constructor() {
        super();
        this._hasAccessToSensitiveData = false;
        // The Property Dataset is local to the active variant, we use this to retrieve the variant we like to gather the value from.
        this.consumeContext(UMB_VARIANT_CONTEXT, async (variantContext) => {
            this.observe(variantContext?.variantId, (variantId) => {
                this._datasetVariantId = variantId;
            }, 'observeDatasetVariantId');
        });
        // The Content Workspace Context is used to retrieve the property type we like to observe.
        // This gives us the configuration from the property type as part of the data type.
        this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, async (workspaceContext) => {
            this._workspaceContext = workspaceContext;
            this.#observePropertyType();
        });
        this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => {
            this.observe(context?.hasAccessToSensitiveData, (hasAccessToSensitiveData) => {
                this._hasAccessToSensitiveData = hasAccessToSensitiveData === true;
            });
        });
    }
    async #observePropertyType() {
        if (!this._alias || !this._workspaceContext)
            return;
        this.observe(await this._workspaceContext?.structure.propertyStructureByAlias(this._alias), (propertyType) => {
            this._propertyType = propertyType;
        }, 'observePropertyType');
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        if (changedProperties.has('_propertyType') || changedProperties.has('_workspaceContext')) {
            const dataTypeUnique = this._propertyType?.dataType.unique;
            if (dataTypeUnique && this._workspaceContext) {
                this.observe(this._workspaceContext.structure.contentTypeDataTypeDetailOf(dataTypeUnique), (detail) => {
                    this._dataTypeDetail = detail;
                }, 'observeDataTypeDetail');
            }
        }
        if (changedProperties.has('_propertyType') ||
            changedProperties.has('_datasetVariantId') ||
            changedProperties.has('_workspaceContext')) {
            if (this._datasetVariantId && this._propertyType && this._workspaceContext) {
                const propertyVariantId = new UmbVariantId(this._propertyType.variesByCulture ? this._datasetVariantId.culture : null, this._propertyType.variesBySegment ? this._datasetVariantId.segment : null);
                this._dataPath = `$.values[${UmbDataPathPropertyValueQuery({
                    alias: this._propertyType.alias,
                    culture: propertyVariantId.culture,
                    segment: propertyVariantId.segment,
                })}].value`;
                this.observe(this._workspaceContext.propertyWriteGuard.isPermittedForVariantAndProperty(propertyVariantId, this._propertyType, this._datasetVariantId), (write) => {
                    this._writeable = write;
                }, 'umbObservePropertyWriteGuard');
                this.observe(this._workspaceContext.propertyViewGuard.isPermittedForVariantAndProperty(propertyVariantId, this._propertyType, this._datasetVariantId), (permitted) => {
                    this._viewable = permitted;
                }, `umbObservePropertyViewGuard`);
            }
        }
    }
    render() {
        if (!this._viewable)
            return nothing;
        if (!this._dataPath || this._writeable === undefined)
            return nothing;
        if (!this._hasAccessToSensitiveData && this._propertyType?.isSensitive)
            return nothing;
        return html `<umb-property-type-based-property
			data-path=${this._dataPath}
			.property=${this._propertyType}
			.dataTypeDetail=${this._dataTypeDetail}
			?readonly=${!this._writeable}></umb-property-type-based-property>`;
    }
};
__decorate([
    property({ type: String, attribute: 'alias' })
], UmbContentWorkspacePropertyElement.prototype, "alias", null);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_datasetVariantId", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_dataPath", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_viewable", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_writeable", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_workspaceContext", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_propertyType", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_dataTypeDetail", void 0);
__decorate([
    state()
], UmbContentWorkspacePropertyElement.prototype, "_hasAccessToSensitiveData", void 0);
UmbContentWorkspacePropertyElement = __decorate([
    customElement('umb-content-workspace-property')
], UmbContentWorkspacePropertyElement);
export { UmbContentWorkspacePropertyElement };
export default UmbContentWorkspacePropertyElement;
