import {
  LinkFieldDefaults,
  TextFieldDefaults,
} from '@hubspot/cms-components/fields';
import { IconContentFieldsWithToggleProps } from '../Icon/types.js';
import { FieldVisibility } from '../utils/types.js';

export type IconPositionHorizontal = 'left' | 'right';

// Base props shared by all variants
export type BaseButtonProps = {
  variant?: 'primaryButton' | 'secondaryButton' | 'tertiaryButton' | 'accentButton';
  className?: string;
  style?: React.CSSProperties;
  children?: React.ReactNode;
  iconFieldPath?: string;
  iconPosition?: IconPositionHorizontal;
  showIcon?: boolean;
  iconPurpose?: 'SEMANTIC' | 'DECORATIVE';
  iconTitle?: string;
};

// Button variant props - requires onClick handler, renders <button> element
// Use this variant when interactive behavior is needed (must be used with JS islands)
export type ButtonAsButtonProps = BaseButtonProps & {
  buttonType: 'button';
  onClick?: () => void;
  disabled?: boolean;
};

// Link variant props - renders <a> element
// Use this variant for navigation without JavaScript
export type ButtonAsLinkProps = BaseButtonProps & {
  buttonType: 'link';
  href?: string;
  target?: '_self' | '_blank' | '_parent' | '_top';
  rel?: string;
};

// Main component props using discriminated union
// TypeScript will provide intelligent autocomplete based on buttonType
export type ButtonProps = ButtonAsButtonProps | ButtonAsLinkProps;

export type ContentFieldsProps<
  TButtonTextName extends string = 'buttonText',
  TButtonLinkName extends string = 'buttonLink',
> = Omit<IconContentFieldsWithToggleProps, 'addIconToggle'> & {
  buttonTextLabel?: string;
  buttonTextName?: TButtonTextName;
  buttonTextDefault?: typeof TextFieldDefaults;
  buttonLinkLabel?: string;
  buttonLinkName?: TButtonLinkName;
  buttonLinkDefault?: typeof LinkFieldDefaults;
  showButtonLink?: boolean;
  buttonIconPositionLabel?: string;
  buttonIconPositionName?: string;
  buttonIconPositionDefault?: IconPositionHorizontal;
} & FieldVisibility<TButtonTextName | TButtonLinkName>;

export type StyleFieldsProps = {
  buttonVariantLabel?: string;
  buttonVariantName?: string;
  buttonVariantDefault?: { variant_name: string };
};

export type RenderWithIconProps = Pick<
  ButtonProps,
  | 'iconFieldPath'
  | 'showIcon'
  | 'iconPurpose'
  | 'iconTitle'
  | 'iconPosition'
> & {
  children: React.ReactNode;
};
