/**
 * NavigationElement Component
 *
 * Navigation button element (navigation_next, navigation_prev).
 * Renders with unified wrapper styling + content.
 */

import React, { useMemo } from 'react';
import type { CSSProperties } from 'react';
import type { CanvasElement } from '../../../types/constructor';
import { resolveAssetPlaybackUrl } from '../../../lib/assetUrl';
import { getFontByKey, getFontStyle } from '../../../lib/fonts';

interface NavigationElementProps {
  element: CanvasElement;
  resolveUrl?: (url: string | undefined) => string;
  className: string;
  style: CSSProperties;
}

const NavigationElement: React.FC<NavigationElementProps> = ({
  element,
  resolveUrl,
  className,
  style,
}) => {
  const resolve = resolveUrl ?? resolveAssetPlaybackUrl;

  // Resolve font key to full CSS style (including fontStretch for condensed variants)
  const labelFontStyle = useMemo(() => {
    const fontKey = element.navLabelFontFamily;
    if (!fontKey) return {};
    const font = getFontByKey(fontKey);
    return font ? getFontStyle(font) : { fontFamily: fontKey };
  }, [element.navLabelFontFamily]);

  // With icon: render image
  if (element.iconUrl) {
    const imgStyle: CSSProperties = {
      width: element.width ? '100%' : 'auto',
      height: element.height ? '100%' : 'auto',
      objectFit: 'contain',
    };

    return (
      <div className={className} style={style}>
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img
          src={resolve(element.iconUrl)}
          alt='Navigation'
          style={imgStyle}
          draggable={false}
        />
      </div>
    );
  }

  // Without icon: render text label
  return (
    <div className={className} style={style}>
      <span className='px-4 py-2 text-sm' style={labelFontStyle}>
        {element.navLabel ||
          (element.type === 'navigation_next' ? 'Next' : 'Back')}
      </span>
    </div>
  );
};

export default NavigationElement;
