"use client";

import { useEffect } from "react";

/**
 * Injects the crop preview's stylesheet once per document.
 *
 * It is a `<style>` element rather than a rule in `globals.css` because the
 * selectors target markup `react-easy-crop` renders itself. Guarded on the id
 * so several croppers on one page do not each add a copy, and removed on the
 * last unmount.
 */
export function useImagePreviewStyles() {
useEffect(() => {
  if (!document.getElementById('image-preview-styles')) {
    const style = document.createElement('style');
    style.id = 'image-preview-styles';
    style.innerHTML = `
      .preview-image {
        display: block;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      .reactEasyCrop_CropAreaGrid {
        background-image: 
          repeating-linear-gradient(
            90deg,
            transparent 0,
            transparent calc(33.33% - 0.5px),
            rgba(255, 255, 255, 0.5) calc(33.33% - 0.5px),
            rgba(255, 255, 255, 0.5) calc(33.33% + 0.5px),
            transparent calc(33.33% + 0.5px),
            transparent calc(66.66% - 0.5px),
            rgba(255, 255, 255, 0.5) calc(66.66% - 0.5px),
            rgba(255, 255, 255, 0.5) calc(66.66% + 0.5px),
            transparent calc(66.66% + 0.5px),
            transparent 100%
          ) !important;
      }
      .reactEasyCrop_CropAreaGrid::before,
      .reactEasyCrop_CropAreaGrid::after {
        content: none !important;
      }
    `;
    document.head.appendChild(style);
  }

  return () => {
    const styleTag = document.getElementById('image-preview-styles');
    if (styleTag) styleTag.remove();
  };
}, []);
}
