import { Metadata } from '@memberjunction/core';
import { UUIDsEqual } from '@memberjunction/global';
import { DashboardEngine } from '@memberjunction/core-entities';
/**
 * Bridges {@link GenericShareDialogComponent} to the dashboard permission model.
 * `MJ: Dashboard Permissions` stores four independent `Can*` booleans; we fold
 * them into the dialog's three-tier `Level` enum and fan back out on save:
 *
 *   View  → CanRead only
 *   Edit  → CanRead + CanEdit
 *   Owner → CanRead + CanEdit + CanDelete + CanShare
 *
 * Refreshes the {@link DashboardEngine} cache after save so downstream
 * consumers see the new grants immediately.
 */
export class DashboardShareAdapter {
    _provider = null;
    get Provider() { return this._provider ?? Metadata.Provider; }
    set Provider(value) { this._provider = value; }
    async LoadShares(context) {
        const existing = DashboardEngine.Instance.GetDashboardShares(context.ResourceID);
        const rows = [];
        const md = this.Provider;
        for (const permission of existing) {
            const user = await md.GetEntityObject('MJ: Users');
            await user.Load(permission.UserID);
            rows.push({
                PermissionEntity: permission,
                UserID: permission.UserID,
                User: user,
                Level: this.flagsToLevel(permission),
                IsNew: false,
                MarkedForRemoval: false
            });
        }
        return rows;
    }
    async CreateShare(context, user) {
        const md = this.Provider;
        const permission = await md.GetEntityObject('MJ: Dashboard Permissions');
        permission.NewRecord();
        permission.DashboardID = context.ResourceID;
        permission.UserID = user.ID;
        this.applyLevelToPermission(permission, 'View');
        if (context.CurrentUserID) {
            permission.SharedByUserID = context.CurrentUserID;
        }
        return {
            PermissionEntity: permission,
            UserID: user.ID,
            User: user,
            Level: 'View',
            IsNew: true,
            MarkedForRemoval: false
        };
    }
    SyncLevelToEntity(row) {
        this.applyLevelToPermission(row.PermissionEntity, row.Level);
    }
    async AfterSave(_context) {
        await DashboardEngine.Instance.Config(true);
    }
    /** Resolve the owner's display name for a dashboard by owner user ID. */
    static resolveOwnerName(ownerUserID) {
        if (!ownerUserID)
            return null;
        const dashboards = DashboardEngine.Instance.Dashboards;
        const match = dashboards.find((d) => d.UserID && UUIDsEqual(d.UserID, ownerUserID));
        return match?.User ?? null;
    }
    flagsToLevel(permission) {
        if (permission.CanDelete || permission.CanShare)
            return 'Owner';
        if (permission.CanEdit)
            return 'Edit';
        return 'View';
    }
    applyLevelToPermission(permission, level) {
        switch (level) {
            case 'Owner':
                permission.CanRead = true;
                permission.CanEdit = true;
                permission.CanDelete = true;
                permission.CanShare = true;
                break;
            case 'Edit':
                permission.CanRead = true;
                permission.CanEdit = true;
                permission.CanDelete = false;
                permission.CanShare = false;
                break;
            case 'View':
            default:
                permission.CanRead = true;
                permission.CanEdit = false;
                permission.CanDelete = false;
                permission.CanShare = false;
                break;
        }
    }
}
//# sourceMappingURL=dashboard-share-adapter.js.map