import { UMB_ENTITY_CONTENT_TYPE_ENTITY_CONTEXT } from '../../context/entity-content-type.context-token.js';
import { UMB_ENTITY_CONTENT_TYPE_UNIQUE_CONDITION_ALIAS } from './constants.js';
import { UmbConditionBase } from '../../../../../core/extension-registry/index.js';
const ObserveSymbol = Symbol();
export class UmbEntityContentTypeUniqueCondition extends UmbConditionBase {
    constructor(host, args) {
        super(host, args);
        let permissionCheck = undefined;
        if (this.config.match) {
            permissionCheck = (unique) => unique === this.config.match;
        }
        else if (this.config.oneOf) {
            permissionCheck = (unique) => (unique ? this.config.oneOf.includes(unique) : false);
        }
        if (permissionCheck !== undefined) {
            this.consumeContext(UMB_ENTITY_CONTENT_TYPE_ENTITY_CONTEXT, (context) => {
                this.observe(context?.unique, (unique) => {
                    this.permitted = permissionCheck(unique);
                }, ObserveSymbol);
            });
        }
        else {
            throw new Error(`Condition [${UMB_ENTITY_CONTENT_TYPE_UNIQUE_CONDITION_ALIAS}] could not be initialized properly. Either "match" or "oneOf" must be defined.`);
        }
    }
}
