import { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";

type $RestProps = SvelteHTMLElements["input"];

type $Props<Icon = any> = {
  /**
   * Specify the value to display and copy.
   * @default ""
   */
  value?: string;

  /**
   * Set to `"password"` to obscure the value.
   * Use `revealMode` to reveal the value on interaction.
   * @default "text"
   */
  type?: "text" | "password";

  /**
   * Control when a `type="password"` value is revealed.
   * Has no effect unless `type` is `"password"`.
   * - `"focus"`: reveal while the input is focused.
   * - `"hover-focus"`: reveal while the input is hovered or focused.
   * When unset, the value stays obscured; the copy button still copies the full value.
   * @default undefined
   */
  revealMode?: "focus" | "hover-focus";

  /**
   * Set to `true` to select the full value when the input receives focus.
   * @default false
   */
  selectOnFocus?: boolean;

  /**
   * Set the size of the input.
   * @default undefined
   */
  size?: "sm" | "xl";

  /**
   * Set to `true` to enable the light variant
   * @default false
   */
  light?: boolean;

  /**
   * Set to `true` to disable the input and copy button
   * @default false
   */
  disabled?: boolean;

  /**
   * Set to `true` to use the inline variant
   * @default false
   */
  inline?: boolean;

  /**
   * Set to `true` to use the fluid variant.
   * Inherited from the parent `FluidForm` context,
   * so it does not need to be set when used inside `FluidForm`.
   * Cannot be combined with the inline variant.
   * @default false
   */
  fluid?: boolean;

  /**
   * Specify the label text
   * @default ""
   */
  labelText?: string;

  /**
   * Set to `true` to visually hide the label text
   * @default false
   */
  hideLabel?: boolean;

  /**
   * Specify the helper text
   * @default ""
   */
  helperText?: string;

  /**
   * Set an id for the input element
   * @default `ccs-${Math.random().toString(36)}`
   */
  id?: string;

  /**
   * Specify a name attribute for the input.
   * @default undefined
   */
  name?: string;

  /**
   * Obtain a reference to the input HTML element.
   * @default null
   */
  ref?: null | HTMLInputElement;

  /**
   * Set the feedback text shown after clicking the copy button
   * @default "Copied!"
   */
  feedback?: string;

  /**
   * Specify an icon to render during the feedback window (for example, after copying).
   * When unset, the copy icon is always shown.
   * @default undefined
   */
  feedbackIcon?: Icon;

  /**
   * Set the timeout duration (ms) to display the feedback text
   * @default 2000
   */
  feedbackTimeout?: number;

  /**
   * Set the title and ARIA label for the copy button
   * @default "Copy to clipboard"
   */
  iconDescription?: string;

  /**
   * Override the default copy behavior of using the navigator.clipboard.writeText API to copy text.
   * @default undefined
   */
  copy?: (text: string) => void | Promise<void>;

  /**
   * Set to `true` to render the feedback tooltip in a portal,
   * preventing it from being clipped by `overflow: hidden` containers.
   * By default, the tooltip is portalled when inside a `Modal`.
   * @default undefined
   */
  portalTooltip?: boolean | undefined;

  /**
   * Set the position of the copy button's tooltip.
   * @default "bottom"
   */
  tooltipPosition?: "top" | "right" | "bottom" | "left";

  /**
   * Set the alignment of the copy button's tooltip.
   * @default "center"
   */
  tooltipAlignment?: "start" | "center" | "end";

  labelChildren?: (this: void) => void;

  [key: `data-${string}`]: unknown;
};

export type CopyInputProps<Icon = any> = Omit<$RestProps, keyof $Props<Icon>> & $Props<Icon>;

export default class CopyInput<Icon = any> extends SvelteComponentTyped<
  CopyInputProps<Icon>,
  {
    blur: WindowEventMap["blur"];
    click: WindowEventMap["click"];
    copy: WindowEventMap["copy"];
    "copy:error": CustomEvent<{ error: unknown }>;
    focus: WindowEventMap["focus"];
    mouseenter: WindowEventMap["mouseenter"];
    "mouseenter:copy-button": CustomEvent<MouseEvent>;
    mouseleave: WindowEventMap["mouseleave"];
    "mouseleave:copy-button": CustomEvent<MouseEvent>;
    mouseover: WindowEventMap["mouseover"];
  },
  { labelChildren: Record<string, never> }
> {}
