import { Coordinate } from 'ol/coordinate';
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { createSelectors } from './createSelectors';

type TTAInfo = {
  jobId: string;
  workingPeriod: [Date, Date];
  duration: number;
  description?: string;
};

interface TTAPlanningState {
  baseInfo?: TTAInfo;
  baseAreaCoordinates?: Coordinate[][];
  setBaseAreaCoordinates: (coordinates: Coordinate[][]) => void;
  reset: () => void;
}

export const useTTAPlanningStoreBase = create<TTAPlanningState>()(
  devtools(
    immer((set) => ({
      baseInfo: undefined,
      baseAreaCoordinates: undefined,
      setBaseAreaCoordinates: (coordinates: Coordinate[][]) =>
        set({ baseAreaCoordinates: coordinates }),
      reset: () =>
        set({
          baseInfo: undefined,
          baseAreaCoordinates: undefined,
        }),
    })),
    {
      name: 'tta-planning-storage',
    },
  ),
);

export const useTTAPlanningStore = createSelectors(useTTAPlanningStoreBase);
