"use client";

import { memo } from "react";
import { getAssetPath, ASSETS } from "../../../../lib/assetUtils";
import type { InputLabelViewProps } from "./InputLabel.types";

function InputLabelView({
  label,
  helpIcon,
  asterisk,
  helperText,
  size,
  palette,
  className = "",
  helpIconAlt,
  helperTextDefault,
}: InputLabelViewProps) {
  const isSmall = size === "s";
  const isInverse = palette === "inverse";

  // Size-based typography
  const labelTextSize = isSmall
    ? "text-small-paragraph"
    : "text-medium-paragraph";

  const helperTextSize = isSmall
    ? "text-xx-small-paragraph"
    : "text-x-small-paragraph";

  const asteriskSize = isSmall
    ? "text-xx-small-label"
    : "font-medium text-[length:var(--measures-spacing-300,12px)] leading-[var(--measures-spacing-300,12px)]";

  // Palette-based colors
  const labelColor = isInverse
    ? "text-[color:var(--color-content-inverse-secondary,#1f1f1f)]"
    : "text-[color:var(--color-content-default-secondary,#d2d2d2)]";

  const helperTextColor =
    "text-[color:var(--color-content-default-tertiary,#b4b4b4)]";

  // Layout: S uses flex-wrap with baseline, M uses flex with center
  const containerClass = isSmall
    ? "flex flex-wrap gap-[var(--measures-spacing-200,4px_8px)] items-baseline pr-[var(--measures-spacing-100,4px)] relative w-full"
    : "flex gap-[4px] items-center relative w-full";

  const labelContainerClass = isSmall
    ? "flex gap-[var(--measures-spacing-050,2px)] items-center relative shrink-0"
    : "flex gap-[var(--measures-spacing-100,4px)] items-center relative shrink-0";

  const helpIconSize = isSmall ? "size-[12px]" : "size-[16px]";

  // Help icon color filter based on palette
  // Default: Light yellow (#f6f06f / rgba(246, 240, 111, 1)) - SVG already has this color
  // Inverse: Dark yellow (#8c8505 / rgba(140, 133, 5, 1))
  // For default, no filter needed as SVG already has the correct yellow
  // For inverse, darken the yellow
  const helpIconFilter = isInverse
    ? "brightness(0.57) saturate(100%)" // Dark yellow (#8c8505) - darken the existing yellow
    : undefined; // No filter for default - use SVG's native yellow color

  return (
    <div className={`${containerClass} ${className}`}>
      <div className={labelContainerClass}>
        <div className="flex gap-px items-start relative shrink-0">
          <p
            className={`${labelTextSize} ${labelColor} relative shrink-0`}
          >
            {label}
          </p>
          {asterisk && (
            <p
              className={`${asteriskSize} relative shrink-0 text-[color:var(--color-content-default-negative-primary,#ea4845)]`}
            >
              *
            </p>
          )}
        </div>
        {helpIcon && (
          <div className={`relative shrink-0 ${helpIconSize}`}>
            {/* eslint-disable-next-line @next/next/no-img-element -- icon from asset path */}
            <img
              src={getAssetPath(ASSETS.ICON_HELP)}
              alt={helpIconAlt}
              className="block max-w-none size-full"
              style={
                helpIconFilter
                  ? {
                      filter: helpIconFilter,
                    }
                  : undefined
              }
            />
          </div>
        )}
      </div>
      {helperText && (
        <p
          className={`flex-[1_0_0] ${helperTextSize} min-h-px min-w-px relative ${helperTextColor} text-right`}
        >
          {typeof helperText === "string" ? helperText : helperTextDefault}
        </p>
      )}
    </div>
  );
}

InputLabelView.displayName = "InputLabelView";

export default memo(InputLabelView);
