import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { ResultContent } from "@model/result/result";

export type ResultStore = {
  results: Result[];
  total_count: number;
};

export type Result = {
  content: ResultContent;
  id: number;
  new: boolean;
  created_at: string;
};

const initialState: ResultStore = {
  results: [],
  total_count: 0,
};

export const useResultStore = create<ResultStore>()(
  persist(
    devtools(() => initialState, {
      name: "ResultStore",
      enabled: process.env.NODE_ENV === "development",
      trace: process.env.NODE_ENV === "development",
    }),
    {
      name: "ResultStore",
    },
  ),
);

//сохранение id результата в массив для отправки на сервер и снятия пометки new с карточки
export const useObsolescenceIdResult = create()(
  persist(
    (set, get) => ({
      idResultArray: [],
      addIdResult: (idResult: number) => {
        const state = get()["idResultArray"] || [];
        const newState = [...state, idResult];
        set({ idResultArray: newState });
      },
      clearIdResultArray: () => {
        const state = get()["idResultArray"] || {};

        const newState = [];
        set({ idResultArray: newState });
      },
    }),
    {
      name: "idResultArr",
    },
  ),
);
