"use client";

import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from "react";
import { Country, Currency, Language } from "@/types/global";
import useSettingsStore from "@/global-store/settings";
import { setCookie } from "cookies-next";
import useAddressStore from "@/global-store/address";
import { defaultLocation } from "@/config/global";

interface SettingsType {
  defaultCurrency?: Currency;
  defaultLanguage?: Language;
  settings?: Record<string, string>;
  currencies?: Currency[];
  defaultCountry?: Country;
}

interface SettingsProviderProps extends PropsWithChildren, SettingsType {}

const SettingsContext = createContext<SettingsType>({});

const SettingsProvider = ({
  defaultCurrency,
  settings,
  defaultLanguage,
  children,
  currencies,
  defaultCountry,
}: SettingsProviderProps) => {
  const selectedCurrency = useSettingsStore((state) => state.selectedCurrency);
  const selectedCountry = useAddressStore((state) => state.country);
  const updateCurrency = useSettingsStore((state) => state.updateSelectedCurrency);
  const updateCountry = useAddressStore((state) => state.updateCountry);
  // const openCountrySelectModal = useAddressStore((state) => state.openCountrySelectModal);
  const updateCity = useAddressStore((state) => state.updateCity);
  const tempSelectedCurrency = currencies?.find((currency) => currency.id === selectedCurrency?.id);

  useEffect(() => {
    if (defaultCountry?.id) {
      if (!selectedCountry?.id || selectedCountry.id !== defaultCountry.id) {
        updateCountry(defaultCountry);
        setCookie("country_id", defaultCountry.id);
      }
      if (!selectedCountry?.id && defaultCountry?.city) {
        updateCity(defaultCountry.city);
        setCookie("city_id", defaultCountry.city.id);
      }
    }
  }, [defaultCountry?.id, defaultCountry?.city?.id, selectedCountry?.id, updateCountry, updateCity]);

  useEffect(() => {
    if (tempSelectedCurrency) {
      updateCurrency(tempSelectedCurrency);
    }
    setCookie("currency_id", tempSelectedCurrency?.id ?? defaultCurrency?.id);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const memoizedValues = useMemo(() => {
    const tempSettings = { ...settings };
    const coordinate = {
      latitude: defaultLocation.lat.toString(),
      longitude: defaultLocation.lng.toString(),
    };
    if (settings?.location) {
      const tempLocation = settings.location.split(",");
      coordinate.latitude = tempLocation[0].trim();
      coordinate.longitude = tempLocation[1].trim();
    }
    if (settings) {
      tempSettings.latitude = coordinate.latitude;
      tempSettings.longitude = coordinate.longitude;
    }
    return { defaultLanguage, defaultCurrency, settings: tempSettings };
  }, [defaultLanguage, defaultCurrency, settings]);

  return <SettingsContext.Provider value={memoizedValues}>{children}</SettingsContext.Provider>;
};

export const useSettingsContext = () => useContext(SettingsContext);

export default SettingsProvider;
