import { UMB_CURRENT_USER_CONTEXT } from '../../current-user/index.js';
import { UmbConditionBase } from '../../../core/extension-registry/index.js';
export class UmbFallbackUserPermissionCondition extends UmbConditionBase {
    constructor(host, args) {
        super(host, args);
        this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => {
            this.observe(context?.currentUser, (currentUser) => {
                const fallbackPermissions = currentUser?.fallbackPermissions || [];
                this.#check(fallbackPermissions);
            }, 'umbUserFallbackPermissionConditionObserver');
        });
    }
    #check(verbs) {
        /* we default to true se we don't require both allOf and oneOf to be defined
         but they can be combined for more complex scenarios */
        let allOfPermitted = true;
        let oneOfPermitted = true;
        // check if all of the verbs are present
        if (this.config.allOf?.length) {
            allOfPermitted = this.config.allOf.every((verb) => verbs.includes(verb));
        }
        // check if at least one of the verbs is present
        if (this.config.oneOf?.length) {
            oneOfPermitted = this.config.oneOf.some((verb) => verbs.includes(verb));
        }
        // if neither allOf or oneOf is defined we default to false
        if (!allOfPermitted && !oneOfPermitted) {
            allOfPermitted = false;
            oneOfPermitted = false;
        }
        this.permitted = allOfPermitted && oneOfPermitted;
    }
}
export { UmbFallbackUserPermissionCondition as api };
