#!/bin/bash

# Create dist directory if it doesn't exist
mkdir -p dist

# Copy the source files to dist
cp src/index.js dist/index.js
cp src/index.js dist/index.esm.js
cp src/cursor.css dist/cursor.css

# Create TypeScript declaration file
cat > dist/index.d.ts << 'EOL'
import { ReactNode } from 'react';

export interface RealtimeCursorOptions {
  apiUrl: string;
  projectId: string;
  user: {
    id: string;
    name: string;
    color?: string;
  };
}

export interface CursorPosition {
  x?: number;
  y?: number;
  relativeX?: number;
  relativeY?: number;
  textPosition?: number;
}

export interface Cursor {
  id: string;
  position: CursorPosition;
  user: {
    id: string;
    name: string;
    color?: string;
  };
  timestamp: number;
}

export interface Collaborator {
  id: string;
  name: string;
  color?: string;
  socketId?: string;
}

export class RealtimeCursor {
  constructor(options: RealtimeCursorOptions);
  connect(): void;
  disconnect(): void;
  updateCursor(position: CursorPosition): void;
  updateContent(content: string, version?: number): void;
  onConnect?: () => void;
  onDisconnect?: () => void;
  onCursorsChange?: (cursors: Record<string, Cursor>) => void;
  onCollaboratorsChange?: (collaborators: Collaborator[]) => void;
  onContentUpdate?: (data: any) => void;
  onUserJoined?: (user: Collaborator) => void;
  onUserLeft?: (user: Collaborator) => void;
  onError?: (error: any) => void;
}

export interface UseRealtimeCursorResult {
  cursors: Record<string, Cursor>;
  collaborators: Collaborator[];
  connected: boolean;
  connect: () => void;
  disconnect: () => void;
  updateCursor: (position: CursorPosition) => void;
  updateContent: (content: string, version?: number) => void;
}

export function useRealtimeCursor(options: RealtimeCursorOptions): UseRealtimeCursorResult;

export interface CursorOverlayProps {
  cursors: Record<string, Cursor>;
  containerRef?: React.RefObject<HTMLElement>;
}

export const CursorOverlay: React.FC<CursorOverlayProps>;

export interface CollaboratorsListProps {
  collaborators: Collaborator[];
}

export const CollaboratorsList: React.FC<CollaboratorsListProps>;

declare const _default: {
  RealtimeCursor: typeof RealtimeCursor;
  useRealtimeCursor: typeof useRealtimeCursor;
  CursorOverlay: typeof CursorOverlay;
  CollaboratorsList: typeof CollaboratorsList;
};

export default _default;
EOL

echo "✅ Build completed successfully!"
echo "📦 Files in dist:"
ls -la dist/