import { Css, PermissionsService } from '@ema/core';
import { Ability } from "@casl/ability";
export class Permission {
    constructor() {
        this.roles = ['ANONYMOUS'];
        this.rules = [];
        this.can = '["read"]';
        this.subject = 'all';
        this.permissions = PermissionsService.instance;
        this.rules = this.permissions.rules;
        addEventListener('permissionsRulesChange', (event) => {
            if (!this.isolate) {
                this.render();
            }
        });
    }
    componentDidLoad() {
        this.displayDefault = Css.getStyle('display', this.el);
        this.parsePermissions();
    }
    componentDidUpdate() {
        this.parsePermissions();
    }
    componentDidUnload() {
        console.log('PermissionComponent removed from the DOM');
    }
    async setPermissionService(instance) {
        this.permissions = instance;
        this.rules = [...this.permissions.rules];
    }
    async getRules() {
        return this.rules;
    }
    async setRules(rules, replace = true) {
        if (replace === true) {
            this.rules = [...rules];
        }
        else {
            this.rules = [...this.rules, ...rules];
        }
    }
    async update() {
        this.render();
    }
    parsePermissions() {
        if (!this.ability) {
            return;
        }
        this.el.style.display = this.displayDefault;
        this.canArray.forEach((action) => {
            if (!this.ability.can(action, this.subject)) {
                this.el.style.display = 'none';
            }
        });
    }
    onPermissionsRulesChange() {
        if (!this.isolate) {
            this.render();
        }
    }
    render() {
        if (this.rulesJson) {
            this.rules = JSON.parse(this.rulesJson);
        }
        if (this.rules.length) {
            this.rules = [...this.permissions.rules];
            this.ability = new Ability(this.rules);
        }
        if (this.can) {
            this.canArray = JSON.parse(this.can);
        }
        if (this.cannot) {
            this.cannotArray = JSON.parse(this.cannot);
        }
        return (h("span", null,
            h("slot", null)));
    }
    static get is() { return "ema-permission"; }
    static get properties() { return {
        "can": {
            "type": String,
            "attr": "can"
        },
        "cannot": {
            "type": String,
            "attr": "cannot"
        },
        "conditions": {
            "type": String,
            "attr": "conditions"
        },
        "el": {
            "elementRef": true
        },
        "getRules": {
            "method": true
        },
        "isolate": {
            "type": Boolean,
            "attr": "isolate"
        },
        "rules": {
            "state": true
        },
        "rulesJson": {
            "type": String,
            "attr": "rules-json"
        },
        "setPermissionService": {
            "method": true
        },
        "setRules": {
            "method": true
        },
        "subject": {
            "type": String,
            "attr": "subject"
        },
        "update": {
            "method": true
        }
    }; }
}
