import { create } from "./Create";

interface Page {
  name: string;
  type: string;
}

type State = {
  pages: Array<Page>;
};

type Actions = {
  setPages: (pages: Array<Page>) => void;
  addPage: (page: Page) => void;
  reset: () => void;
};

// define the initial state
const initialState: State = {
  pages: [],
};

const usePages = create<State & Actions>()((set, get) => ({
  ...initialState,

  setPages: (pages: Array<Page>) => {
    set({ pages });
  },

  addPage: (page: Page) => {
    set((state) => ({
      pages: [...state.pages, page],
    }));
  },

  reset: () => {
    set(initialState);
  },
}));

export { usePages };
export type { Page };
