import path from 'node:path';
export class EditOwnershipTracker {
    ownerships = new Map();
    log;
    emit;
    constructor(log, emit) {
        this.log = log.child({ component: 'EditOwnershipTracker' });
        this.emit = emit;
    }
    normalizePath(filePath) {
        return path.resolve(filePath);
    }
    claim(filePath, owner) {
        const normalized = this.normalizePath(filePath);
        const existing = this.ownerships.get(normalized);
        if (existing && existing.owner !== owner) {
            this.log.debug('ownership claim denied', {
                filePath: normalized,
                requester: owner,
                currentOwner: existing.owner,
            });
            this.emit({
                type: 'ownership_denied',
                filePath: normalized,
                requester: owner,
                currentOwner: existing.owner,
            });
            return { claimed: false, currentOwner: existing.owner, filePath: normalized };
        }
        if (existing && existing.owner === owner) {
            return { claimed: true, ownership: existing };
        }
        const ownership = {
            filePath: normalized,
            owner,
            claimedAt: Date.now(),
        };
        this.ownerships.set(normalized, ownership);
        this.log.debug('ownership claimed', { filePath: normalized, owner });
        this.emit({ type: 'ownership_claimed', filePath: normalized, owner });
        return { claimed: true, ownership };
    }
    release(filePath, owner) {
        const normalized = this.normalizePath(filePath);
        const existing = this.ownerships.get(normalized);
        if (!existing || existing.owner !== owner) {
            return false;
        }
        this.ownerships.delete(normalized);
        this.log.debug('ownership released', { filePath: normalized, previousOwner: owner });
        this.emit({ type: 'ownership_released', filePath: normalized, previousOwner: owner });
        return true;
    }
    transfer(filePath, from, to) {
        const normalized = this.normalizePath(filePath);
        const existing = this.ownerships.get(normalized);
        if (!existing || existing.owner !== from) {
            return false;
        }
        const transferred = {
            filePath: normalized,
            owner: to,
            claimedAt: Date.now(),
        };
        this.ownerships.set(normalized, transferred);
        this.log.info('ownership transferred', { filePath: normalized, from, to });
        this.emit({ type: 'ownership_transferred', filePath: normalized, from, to });
        return true;
    }
    releaseAll(owner) {
        let released = 0;
        for (const [normalized, ownership] of this.ownerships) {
            if (ownership.owner === owner) {
                this.ownerships.delete(normalized);
                this.log.debug('ownership released (cleanup)', {
                    filePath: normalized,
                    previousOwner: owner,
                });
                this.emit({
                    type: 'ownership_released',
                    filePath: normalized,
                    previousOwner: owner,
                });
                released += 1;
            }
        }
        return released;
    }
    getOwner(filePath) {
        const normalized = this.normalizePath(filePath);
        return this.ownerships.get(normalized)?.owner;
    }
    listByOwner(owner) {
        const result = [];
        for (const ownership of this.ownerships.values()) {
            if (ownership.owner === owner) {
                result.push(ownership);
            }
        }
        return result;
    }
    checkConflict(filePath, requester) {
        const normalized = this.normalizePath(filePath);
        const existing = this.ownerships.get(normalized);
        if (existing && existing.owner !== requester) {
            return existing.owner;
        }
        return undefined;
    }
}
//# sourceMappingURL=ownership.js.map