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, RunView } from '@memberjunction/core';
import { RegisterClass, UUIDsEqual } from '@memberjunction/global';
import { MJArtifactPermissionEntity } from '../../generated/entity_subclasses.js';
import { assertCallerMayCreateShare, buildActionsSummary, checkShareManagePermission, dispatchShareNotificationAfterSave, } from './BaseShareEntityExtended.js';
/**
 * Extended Artifact Permission entity — same dual-purpose pattern as
 * {@link MJDashboardPermissionEntityExtended}. See that class for rationale.
 */
let MJArtifactPermissionEntityExtended = class MJArtifactPermissionEntityExtended extends MJArtifactPermissionEntity {
    CheckPermissions(type, throwError) {
        if (type === EntityPermissionType.Update || type === EntityPermissionType.Delete) {
            const user = this.ActiveUser;
            if (user && checkShareManagePermission(user, this.SharedByUserID))
                return true;
        }
        return super.CheckPermissions(type, throwError);
    }
    async Save(options) {
        const isNewShare = !this.IsSaved;
        const allowed = await assertCallerMayCreateShare(this, isNewShare, () => this.callerMayShareArtifact(), 'Only the artifact owner or someone with Share permission on this artifact can create a new share.');
        if (!allowed)
            return false;
        const saved = await super.Save(options);
        if (saved) {
            await dispatchShareNotificationAfterSave(this, isNewShare, this.SharedByUserID, async (provider, grantorId) => {
                // The Artifact view doesn't denormalize Name onto the permission row — look it up.
                const artifactName = await this.fetchArtifactName();
                return {
                    Provider: provider,
                    ContextUser: this.ContextCurrentUser,
                    GrantorUserID: grantorId,
                    GranteeUserID: this.UserID,
                    ResourceTypeLabel: 'Artifact',
                    ResourceTypeName: 'Artifacts',
                    ResourceName: artifactName,
                    ResourceRecordID: this.ArtifactID,
                    ActionsSummary: this.actionsSummary(),
                    ExtraConfiguration: { PermissionID: this.ID },
                };
            });
        }
        return saved;
    }
    actionsSummary() {
        return buildActionsSummary({
            view: this.CanRead,
            edit: this.CanEdit,
            delete: this.CanDelete,
            share: this.CanShare,
        });
    }
    /**
     * Authorization for CREATING a new ArtifactPermission row.
     * Same pattern as MJDashboardPermissionEntityExtended.callerMayShareDashboard
     * but without an in-memory engine — we fetch the artifact and the caller's
     * existing grant row directly.
     */
    async callerMayShareArtifact() {
        const user = this.ContextCurrentUser;
        if (!user)
            return false;
        const rv = new RunView();
        // One round-trip: fetch the artifact's owner AND any existing Share-capable
        // permission row for this caller on this artifact.
        const [ownerResult, grantResult] = await rv.RunViews([
            {
                EntityName: 'MJ: Artifacts',
                ExtraFilter: `ID='${this.ArtifactID}'`,
                Fields: ['ID', 'UserID'],
                MaxRows: 1,
                ResultType: 'simple',
            },
            {
                EntityName: 'MJ: Artifact Permissions',
                ExtraFilter: `ArtifactID='${this.ArtifactID}' AND UserID='${user.ID}' AND CanShare=1`,
                Fields: ['ID'],
                MaxRows: 1,
                ResultType: 'simple',
            },
        ]);
        const ownerId = ownerResult.Success
            ? ownerResult.Results?.[0]?.UserID
            : undefined;
        if (ownerId && UUIDsEqual(ownerId, user.ID))
            return true;
        return grantResult.Success && (grantResult.Results?.length ?? 0) > 0;
    }
    async fetchArtifactName() {
        const rv = new RunView();
        const result = await rv.RunView({
            EntityName: 'MJ: Artifacts',
            ExtraFilter: `ID='${this.ArtifactID}'`,
            Fields: ['ID', 'Name'],
            MaxRows: 1,
            ResultType: 'simple',
        });
        return result.Success ? result.Results?.[0]?.Name ?? null : null;
    }
};
MJArtifactPermissionEntityExtended = __decorate([
    RegisterClass(BaseEntity, 'MJ: Artifact Permissions')
], MJArtifactPermissionEntityExtended);
export { MJArtifactPermissionEntityExtended };
/** Tree-shaking prevention — referenced from the custom/Permissions barrel. */
export function LoadMJArtifactPermissionEntityExtended() {
    // intentionally empty
}
//# sourceMappingURL=MJArtifactPermissionEntityExtended.js.map