export class TeamLockService {
    locks = new Map();
    getRequiredLocks(request) {
        const lockKeys = new Set();
        const usesWorkspaceLocks = request.workspacePolicy === 'shared' && request.workspaceId != null;
        if (usesWorkspaceLocks && request.capabilities.writeFiles) {
            lockKeys.add(`workspace:${request.workspaceId}:write`);
        }
        if (usesWorkspaceLocks && request.capabilities.runCommands) {
            lockKeys.add(`workspace:${request.workspaceId}:command`);
        }
        if (request.capabilities.mergeWorkspace && request.projectId != null) {
            lockKeys.add(`project:${request.projectId}:merge`);
        }
        return [...lockKeys];
    }
    canAcquire(lockKeys) {
        return lockKeys.every((key) => !this.locks.has(key));
    }
    acquire(ownerId, lockKeys) {
        const uniqueKeys = [...new Set(lockKeys)];
        const canAcquireAll = uniqueKeys.every((key) => {
            const currentOwnerId = this.locks.get(key);
            return currentOwnerId == null || currentOwnerId === ownerId;
        });
        if (!canAcquireAll) {
            return false;
        }
        for (const key of uniqueKeys) {
            this.locks.set(key, ownerId);
        }
        return true;
    }
    isHeldBy(ownerId, lockKey) {
        return this.locks.get(lockKey) === ownerId;
    }
    release(ownerId, lockKeys) {
        for (const key of lockKeys) {
            if (this.locks.get(key) === ownerId) {
                this.locks.delete(key);
            }
        }
    }
    releaseByOwner(ownerId) {
        for (const [key, currentOwnerId] of this.locks) {
            if (currentOwnerId === ownerId) {
                this.locks.delete(key);
            }
        }
    }
    listLocks() {
        return [...this.locks.entries()].map(([key, ownerId]) => ({ key, ownerId }));
    }
}
export const defaultTeamLockService = new TeamLockService();
//# sourceMappingURL=team-lock.service.js.map