import { create } from "zustand";

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

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

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

export default generatedArticleIds;
