import { FC, useRef, useState, useEffect } from 'react';
import useIsotope from '@/hooks/useIsotope';

import classNames from 'classnames';
import { useLightBoxGallery } from 'hooks/useLightBox';
import GalleryPhotos from '@/components/templates/layout/Gallery';
interface GalleryProps {
  title?: string;
  preTitle?: string;
  categories: any[];
  darkColor?: string;
  lightColor?: string;
  gallery: any[];
  classNameId?: string;
  trans: any;
  id?: string;
}
const Gallery: FC<GalleryProps> = (props) => {
  const {
    id = 'gallery',
    title = '',
    preTitle = '',
    darkColor = '',
    lightColor = '',
    categories,
    gallery,
    classNameId = 'isotope',
    trans
  } = props;

  const { filterKey, handleFilterKeyChange, updateIsotope } = useIsotope('.' + classNameId);
  const [visibleItems, setVisibleItems] = useState(6); // Number of visible items
  const containerRef = useRef<HTMLDivElement>(null);
  useLightBoxGallery([visibleItems]);
  // Filter options

  // Load more items
  const handleLoadMore = () => {
    setVisibleItems((prev) => prev + 6);
  };

  // Reinitialize Isotope when visibleItems change
  useEffect(() => {
    if (containerRef.current) {
      updateIsotope(filterKey); // Trigger relayout after items are appended
    }
  }, [visibleItems, updateIsotope]);

  return (
    <section id={id} style={{ background: lightColor }}>
      <div className="wrapper">
        <div className="container py-7 py-md-14 text-center">
          <div className="col-12 tw-text-start pb-4 pb-lg-8">
            <p className="base-pretitle text-center mb-2" style={{ color: darkColor }}>
              {preTitle}
            </p>
            <h2 style={{ color: darkColor }} className="base-title text-center">
              {title}
            </h2>
          </div>
          <GalleryPhotos categories={categories} images={gallery} activeColor={darkColor} borderColor={darkColor} />
        </div>
      </div>
    </section>
  );
};

export default Gallery;
