"use client";

import { useEffect } from "react";

export default function GeneralSettingsLoader() {
  useEffect(() => {
    const loadSettings = async () => {
      try {
        const response = await fetch(
          "/api/general-settings"
        );

        const data = await response.json();
        const settings = data?.data;

        if (settings) {
          localStorage.setItem("general-settings", JSON.stringify(settings));
          localStorage.setItem("Generalsettings", JSON.stringify(settings));
          window.dispatchEvent(new Event("settings-loaded"));
        }
      } catch (error) {
        console.error(
          "Failed to load general settings",
          error
        );
      }
    };

    loadSettings();
  }, []);

  return null;
}
export const generalsettingsdata = () => {
  if (typeof window === "undefined") {
    return null;
  }

  const settings = localStorage.getItem("general-settings");

  return settings ? JSON.parse(settings) : null;
};