import { useToken } from "@/hooks/useToken";
import { Client } from "@liveblocks/client";
import { createRoomContext } from "@liveblocks/react";
import React from "react";
import { v4 as uuidv4 } from "uuid";

export const classNames = (...classes: Array<string>) => {
  return classes.filter(Boolean).join(" ");
};

export const getChildByName = (children: React.ReactNode, name: string) => {
  return React.Children.toArray(children).filter(
    (x: any) => x.type.name === name
  );
};

export const truncate = (str: string, size: number = 10) => {
  if (!str || str.length === 0) return str;
  return str.length > size ? str.substring(0, size - 3) + "..." : str;
};

export const useUniqueId = () => {
  return uuidv4();
};

export const letterCase = (str: string) => {
  if (!str || str.length === 0) return str;
  return str.charAt(0).toUpperCase() + str.slice(1);
};

export const fetcher = (url: string) =>
  fetch(url)
    .then((res) => res.json())
    .catch((e) => console.log(e));

//export const api_endpoint = "https://api.edgebrix.com/v1";

export const api_endpoint = "http://localhost:3002/api/v1";

type Presence = {
  cursor: { x: number; y: number } | null;
};

export const liveblocks = (client: Client | any) => {
  return createRoomContext<Presence>(client);
};
