/* Letter.jsx — stretched-glyph column header.
   The middle slice scales horizontally with the column width;
   left and right edges stay fixed-size. */

function Letter({ ch, color = "#1C1C1C" }) {
  // Geometry: each letter rendered as 3 SVGs side-by-side — left edge,
  // middle (preserveAspectRatio=none stretch), right edge.
  // Heights normalised to 150px header zone, with 16px margin.
  const STROKE = 22;     // bar thickness
  const H = 130;
  const LEFT_W = 26;
  const RIGHT_W = 26;

  const variants = {
    B: {
      left:  <svg viewBox={`0 0 ${LEFT_W} ${H}`} preserveAspectRatio="xMinYMid meet" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width={STROKE} height={H} fill={color} />
      </svg>,
      mid:   <svg viewBox={`0 0 100 ${H}`} preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width="100" height={STROKE} fill={color} />
        <rect x="0" y={H/2 - STROKE/2} width="100" height={STROKE} fill={color} />
        <rect x="0" y={H - STROKE} width="100" height={STROKE} fill={color} />
      </svg>,
      right: <svg viewBox={`0 0 ${RIGHT_W} ${H}`} preserveAspectRatio="xMaxYMid meet" xmlns="http://www.w3.org/2000/svg">
        <path d={`M0 0 H${STROKE} a${RIGHT_W-STROKE} ${H/4} 0 0 1 0 ${H/2 - STROKE/2} H0 Z`} fill={color}/>
        <path d={`M0 ${H/2 + STROKE/2} H${STROKE} a${RIGHT_W-STROKE} ${H/4} 0 0 1 0 ${H/2 - STROKE/2} H0 Z`} fill={color}/>
      </svg>,
    },
    O: {
      left:  <svg viewBox={`0 0 ${LEFT_W} ${H}`} preserveAspectRatio="xMinYMid meet" xmlns="http://www.w3.org/2000/svg">
        <path d={`M${LEFT_W} 0 H${STROKE} A${LEFT_W} ${H/2} 0 0 0 ${STROKE} ${H} H${LEFT_W} Z M${LEFT_W} ${STROKE} V${H-STROKE}`} fill={color}/>
        <rect x="0" y="0" width={STROKE} height={STROKE} fill={color}/>
        <rect x="0" y={H-STROKE} width={STROKE} height={STROKE} fill={color}/>
      </svg>,
      mid:   <svg viewBox={`0 0 100 ${H}`} preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width="100" height={STROKE} fill={color}/>
        <rect x="0" y={H-STROKE} width="100" height={STROKE} fill={color}/>
      </svg>,
      right: <svg viewBox={`0 0 ${RIGHT_W} ${H}`} preserveAspectRatio="xMaxYMid meet" xmlns="http://www.w3.org/2000/svg">
        <path d={`M0 0 H${RIGHT_W-STROKE} A${RIGHT_W} ${H/2} 0 0 1 ${RIGHT_W-STROKE} ${H} H0 V${H-STROKE} H${RIGHT_W-STROKE} A${RIGHT_W-STROKE*2} ${H/2-STROKE} 0 0 0 ${RIGHT_W-STROKE} ${STROKE} H0 Z`} fill={color}/>
      </svg>,
    },
    D: {
      left:  <svg viewBox={`0 0 ${LEFT_W} ${H}`} preserveAspectRatio="xMinYMid meet" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width={STROKE} height={H} fill={color} />
      </svg>,
      mid:   <svg viewBox={`0 0 100 ${H}`} preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width="100" height={STROKE} fill={color}/>
        <rect x="0" y={H-STROKE} width="100" height={STROKE} fill={color}/>
      </svg>,
      right: <svg viewBox={`0 0 ${RIGHT_W} ${H}`} preserveAspectRatio="xMaxYMid meet" xmlns="http://www.w3.org/2000/svg">
        <path d={`M0 0 H${RIGHT_W/2} A${RIGHT_W/2} ${H/2} 0 0 1 ${RIGHT_W/2} ${H} H0 V${H-STROKE} H${RIGHT_W/2 - STROKE/2} A${RIGHT_W/2 - STROKE} ${H/2 - STROKE} 0 0 0 ${RIGHT_W/2 - STROKE/2} ${STROKE} H0 Z`} fill={color}/>
      </svg>,
    },
    E: {
      left:  <svg viewBox={`0 0 ${LEFT_W} ${H}`} preserveAspectRatio="xMinYMid meet" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width={STROKE} height={H} fill={color} />
      </svg>,
      mid:   <svg viewBox={`0 0 100 ${H}`} preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width="100" height={STROKE} fill={color}/>
        <rect x="0" y={H/2 - STROKE/2} width="100" height={STROKE} fill={color}/>
        <rect x="0" y={H-STROKE} width="100" height={STROKE} fill={color}/>
      </svg>,
      right: <svg viewBox={`0 0 ${RIGHT_W} ${H}`} preserveAspectRatio="xMaxYMid meet" xmlns="http://www.w3.org/2000/svg">
        <rect x="0" y="0" width={STROKE} height={STROKE} fill={color}/>
        <rect x="0" y={H/2 - STROKE/2} width={STROKE} height={STROKE} fill={color}/>
        <rect x="0" y={H-STROKE} width={STROKE} height={STROKE} fill={color}/>
      </svg>,
    },
  };

  const v = variants[ch];
  if (!v) return null;
  return (
    <div className="letter">
      <div style={{ flex: `0 0 ${LEFT_W}px`, height: '100%' }}>{v.left}</div>
      <div style={{ flex: 1, height: '100%' }}>{v.mid}</div>
      <div style={{ flex: `0 0 ${RIGHT_W}px`, height: '100%' }}>{v.right}</div>
    </div>
  );
}

window.Letter = Letter;
