"use client";

import { HexColorInput } from "react-colorful";
import { cn } from "@/lib/cn";
import { PopoverPicker } from "@/modules/ui/components/color-picker/components/popover-picker";

interface ColorPickerProps {
  color: string;
  onChange: (v: string) => void;
  containerClass?: string;
  disabled?: boolean;
  placeholder?: string;
  /** Shown greyed in the input and in the swatch while `color` is empty (e.g. a derived dark color). */
  placeholderColor?: string;
  /**
   * The hex input's id, for a `<label htmlFor>` that names it. Without one the input is named
   * "Primary color".
   */
  id?: string;
}
export const ColorPicker = ({
  color,
  onChange,
  containerClass,
  disabled = false,
  placeholder,
  placeholderColor,
  id,
}: Readonly<ColorPickerProps>) => {
  return (
    <div className={cn(containerClass)}>
      <div className="flex h-10 w-full items-center gap-2 rounded-md border border-slate-300 bg-white px-2 text-sm text-slate-400">
        #
        <HexColorInput
          className="min-w-0 flex-1 border-0 bg-transparent text-slate-500 outline-hidden focus:border-none"
          color={color}
          onChange={onChange}
          id={id ?? "color"}
          aria-label={id ? undefined : "Primary color"}
          disabled={disabled}
          placeholder={placeholder ?? placeholderColor?.replace(/^#/, "")}
        />
        <PopoverPicker color={color || placeholderColor || ""} onChange={onChange} disabled={disabled} />
      </div>
    </div>
  );
};
