import { create } from "zustand";

interface targetStore {
  articleIds: { singleArticle: number[]; bulkArticle: number[] };
  setArticleIds: (newTarget: {
    singleArticle: number[];
    bulkArticle: number[];
  }) => void;
}

const getInitialTarget = () => {
  if (typeof localStorage !== "undefined") {
    const stored = localStorage.getItem("generated-article");
    const initial = stored
      ? JSON.parse(stored)
      : { singleArticle: [], bulkArticle: [] };
    return initial;
  }
};

const generatedArticleIds = create<targetStore>((set) => ({
  articleIds: getInitialTarget(),
  setArticleIds: (newTarget: {
    singleArticle: number[];
    bulkArticle: number[];
  }) => {
    localStorage.setItem("generated-article", JSON.stringify(newTarget));
    set({ articleIds: newTarget });
  },
}));

export default generatedArticleIds;
