import { create } from "zustand";

interface targetStore {
  locale: string;
  setLocale: (newTarget: string) => void;
}

const getInitialTarget = () => {
  if (typeof localStorage !== "undefined") {
    const stored = localStorage.getItem("locale");
    const initial = stored ? JSON.parse(stored) : "id";
    return initial;
  }
};

const storedLanguage = create<targetStore>((set) => ({
  locale: getInitialTarget(),
  setLocale: (newTarget: string) => {
    localStorage.setItem("locale", JSON.stringify(newTarget));
    set({ locale: newTarget });
  },
}));

export default storedLanguage;
