import lockfile from 'proper-lockfile';
/**
 * Shared lock options so sync and async locks always use the same lock path.
 * realpath: false ensures both create the same .lock directory on Windows,
 * where fs.realpath can normalize paths differently.
 */
export const LOCK_BASE_OPTIONS = { realpath: false };
const SYNC_LOCK_RETRY_ATTEMPTS = 10;
const SYNC_LOCK_RETRY_MIN_TIMEOUT_MS = 100;
const SYNC_LOCK_RETRY_MAX_TIMEOUT_MS = 1000;
function sleepSync(ms) {
    Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}
function isLockContentionError(error) {
    return error instanceof Error && error.code === 'ELOCKED';
}
export function lockSyncWithRetries(filePath) {
    let delayMs = SYNC_LOCK_RETRY_MIN_TIMEOUT_MS;
    for (let attempt = 0;; attempt += 1) {
        try {
            return lockfile.lockSync(filePath, LOCK_BASE_OPTIONS);
        }
        catch (error) {
            if (!isLockContentionError(error) || attempt >= SYNC_LOCK_RETRY_ATTEMPTS) {
                throw error;
            }
            sleepSync(delayMs);
            delayMs = Math.min(delayMs * 2, SYNC_LOCK_RETRY_MAX_TIMEOUT_MS);
        }
    }
}
//# sourceMappingURL=lock-utils.js.map