export class ClipboardManager {
  constructor({ idFactory, offset = 20 } = {}) {
    this.idFactory = idFactory || (() => `stroke-${Date.now()}-${Math.random().toString(36).slice(2)}`);
    this.offset = offset;
    this.strokes = [];
  }

  copy(strokes) {
    this.strokes = strokes.map((stroke) => structuredClone(stroke));
    return this.strokes.length;
  }

  hasData() {
    return this.strokes.length > 0;
  }

  paste({ dx = this.offset, dy = this.offset } = {}) {
    return this.strokes.map((stroke) => {
      const clone = structuredClone(stroke);
      clone.id = this.idFactory();
      if (Array.isArray(clone.points)) {
        clone.points.forEach((point) => {
          point.x += dx;
          point.y += dy;
        });
      }
      ["x", "startX", "endX"].forEach((key) => {
        if (Number.isFinite(clone[key])) {
          clone[key] += dx;
        }
      });
      ["y", "startY", "endY"].forEach((key) => {
        if (Number.isFinite(clone[key])) {
          clone[key] += dy;
        }
      });
      return clone;
    });
  }
}
