'use strict';

class ProxyLease {
  constructor(registry, opts = {}) {
    this.registry = registry;
    this.size = opts.size || 0;
    this.strategy = opts.strategy || null;
    this.leaseId = `lease_${Date.now()}_${Math.floor(Math.random() * 1e6)}`;
    this.allocated = new Map();
  }

  pick(excludeKeys) {
    const strategy = this.strategy || this.registry.defaultStrategy;
    const entry = this.registry.pick({ strategy, excludeKeys, leaseId: this.leaseId });
    if (entry) {
      this.allocated.set(entry.key, entry);
    }
    return entry;
  }

  release(key) {
    this.registry.release(key);
    this.allocated.delete(key);
  }

  returnAll() {
    for (const key of this.allocated.keys()) {
      this.registry.release(key);
    }
    this.allocated.clear();
  }
}

module.exports = { ProxyLease };
