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 { BaseEntity, EntityPermissionType } from '@memberjunction/core';
import { RegisterClass } from '@memberjunction/global';
import { MJAccessControlRuleEntity } from '../../generated/entity_subclasses.js';
import { buildActionsSummary, checkShareManagePermission, dispatchShareNotificationAfterSave, } from './BaseShareEntityExtended.js';
/**
 * Extended Access Control Rule entity — same dual-purpose pattern as
 * {@link MJDashboardPermissionEntityExtended}, with two differences:
 *
 *  - Uses `GrantedByUserID` instead of `SharedByUserID` (ACR column naming).
 *  - Only notifies when `GranteeType === 'User'` and `GranteeID` is populated.
 *    Role / Everyone / Public grants don't have a single identifiable recipient,
 *    so the payload builder returns `null` to skip.
 *
 * Notifications use the generic `Records` Resource Type from the catalog because
 * ACRs target arbitrary entities — there's no single ResourceType that fits.
 */
let MJAccessControlRuleEntityExtended = class MJAccessControlRuleEntityExtended extends MJAccessControlRuleEntity {
    CheckPermissions(type, throwError) {
        if (type === EntityPermissionType.Update || type === EntityPermissionType.Delete) {
            const user = this.ActiveUser;
            if (user && checkShareManagePermission(user, this.GrantedByUserID))
                return true;
        }
        return super.CheckPermissions(type, throwError);
    }
    async Save(options) {
        const isNewShare = !this.IsSaved;
        const saved = await super.Save(options);
        if (saved) {
            await dispatchShareNotificationAfterSave(this, isNewShare, this.GrantedByUserID, (provider, grantorId) => {
                // Only user grantees get individual notifications.
                if (this.GranteeType !== 'User' || !this.GranteeID)
                    return null;
                const entityInfo = provider.EntityByID(this.EntityID);
                return {
                    Provider: provider,
                    ContextUser: this.ContextCurrentUser,
                    GrantorUserID: grantorId,
                    GranteeUserID: this.GranteeID,
                    ResourceTypeLabel: entityInfo?.Name ?? 'Record',
                    ResourceTypeName: 'Records',
                    ResourceName: null,
                    ResourceRecordID: this.RecordID,
                    ActionsSummary: this.actionsSummary(),
                    ExtraConfiguration: { AccessControlRuleID: this.ID, EntityID: this.EntityID },
                };
            });
        }
        return saved;
    }
    actionsSummary() {
        return buildActionsSummary({
            view: this.CanRead,
            create: this.CanCreate,
            edit: this.CanUpdate,
            delete: this.CanDelete,
            share: this.CanShare,
        });
    }
};
MJAccessControlRuleEntityExtended = __decorate([
    RegisterClass(BaseEntity, 'MJ: Access Control Rules')
], MJAccessControlRuleEntityExtended);
export { MJAccessControlRuleEntityExtended };
/** Tree-shaking prevention — referenced from the custom/Permissions barrel. */
export function LoadMJAccessControlRuleEntityExtended() {
    // intentionally empty
}
//# sourceMappingURL=MJAccessControlRuleEntityExtended.js.map