import React from 'react';
import './ImagePreview.css';

export default function ImagePreview({ images = [], visible = false, index = 0, onClose, onChange }) {
  if (!visible) return null;
  const len = images.length || 1;
  const go = dir => onChange && onChange((index + dir + len) % len);
  return (
    <div className="kole-imgprev-mask" onClick={e => { if (e.target === e.currentTarget) onClose && onClose(); }}>
      <button className="kole-imgprev-close" onClick={onClose}>✕</button>
      <button className="kole-imgprev-arrow prev" onClick={() => go(-1)}>‹</button>
      <img className="kole-imgprev-img" src={images[index]} alt={index} />
      <button className="kole-imgprev-arrow next" onClick={() => go(1)}>›</button>
      <div className="kole-imgprev-count">{index + 1} / {len}</div>
      <div className="kole-imgprev-thumbs">
        {images.map((u, i) => (
          <img key={i} className={'kole-imgprev-thumb' + (i === index ? ' is-active' : '')}
               src={u} onClick={() => onChange && onChange(i)} />
        ))}
      </div>
    </div>
  );
}
