import {
  createRenderEffect,
  onCleanup,
  type Accessor,
} from "solid-js";

export function useDocumentTitle(newTitle?: string | Accessor<string>) {
  const capturedTitle = document.title;

  createRenderEffect(() => {
    if (newTitle)
      document.title = typeof newTitle === "string" ? newTitle : newTitle();
  });

  onCleanup(() => {
    document.title = capturedTitle;
  });

  return (x: ((x: string) => string) | string) =>
    (document.title = typeof x === "string" ? x : x(document.title));
}

export function mergeClass(c1: string | undefined, c2: string | undefined) {
  if (!c1) {
    return c2;
  }
  if (!c2) {
    return c1;
  }
  return [c1, c2].join(" ");
}
