"use client";

import Image from "next/image";

/** Resolve a toolbar icon's public path. */
export const asset = (path: string) => `/assets/description_icons/${path}`;

/** Toolbar icon button that reflects an active mark. */
export default function ToolbarButton({
  onClick,
  active,
  src,
  alt,
  size = 24,
}: {
  onClick: () => void;
  active?: boolean;
  src: string;
  alt: string;
  size?: number;
}) {
  return (
    <button
      type="button"
      /* `aria-pressed` only where `active` is actually passed: undo and redo
         are plain actions, not toggles, and announcing them as "not pressed"
         would be wrong. Bold/italic/underline/strike and the two list buttons
         do pass it, so a screen reader can tell whether the mark is on. */
      aria-pressed={active}
      onClick={onClick}
      className={`p-1 rounded ${
        active ? "bg-gray-300 hover:bg-gray-300" : "hover:bg-gray-200"
      }`}
    >
      <Image
        src={src}
        alt={alt}
        width={size}
        height={size}
        style={{ width: size, height: size }}
      />
    </button>
  );
}

/** The thin rule that groups the toolbar's buttons. */
export const separator = (key: string) => (
  <span className="p-1 rounded" key={key}>
    <Image
      src={asset("line.svg")}
      alt=""
      width={8}
      height={20}
      style={{ width: 8, height: 20 }}
    />
  </span>
);
