import { createStore } from 'zustand'
import type {
  MeasurementToolsProviderProps,
  MeasurementToolsStore,
} from './types'
import {
  DEFAULT_MEASUREMENT_TOOLS_MODES,
  DEFAULT_MEASUREMENT_TOOLS_UNITS,
  MapMeasurementUnitAuto,
} from '../const'
import { CustomMeasureDistanceMode } from '../modes/custom-measurement-distance.class'

/**
 * Creates a Zustand store for managing measurement tools state across multiple map instances.
 *
 * @param initProps - Array of initial measurement tool configurations, one per map instance.
 * @returns A Zustand store with measurement state and actions (setEnabled, setModeSelected, setModeInstance, setFinished, setDistance, setUnit, setLocale, setPositions).
 */
export const createMeasurementToolsStore = (
  initProps: MeasurementToolsProviderProps['values'],
) => {
  return createStore<MeasurementToolsStore>()((set) => {
    const defaultProps = initProps.reduce(
      (acc, curr) => {
        const {
          mapId,
          modeSelected,
          modes = DEFAULT_MEASUREMENT_TOOLS_MODES,
          units = DEFAULT_MEASUREMENT_TOOLS_UNITS,
          distance = 0,
          finished = false,
          locale = 'en',
          positions = [],
          clearLayerOnStart = false,
          unit = MapMeasurementUnitAuto['m-km'],
          enabled = false,
        } = curr
        acc[mapId] = {
          modes,
          units,
          modeSelected,
          distance,
          finished,
          locale,
          positions,
          unit,
          clearLayerOnStart,
          enabled,
        }
        return acc
      },
      {} as MeasurementToolsStore['measurements'],
    )

    return {
      measurements: defaultProps,
      mode: CustomMeasureDistanceMode,
      setModeInstance: (mapId, modeInstance) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              modeInstance,
            },
          },
        }))
      },
      setModeSelected: (mapId, modeSelected) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              modeSelected,
            },
          },
        }))
      },
      setFinished: (mapId, finished) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              finished,
            },
          },
        }))
      },
      setDistance: (mapId, distance) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              distance,
            },
          },
        }))
      },
      setUnit: (mapId, unit) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              unit,
            },
          },
        }))
      },
      setLocale: (mapId, locale) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              locale,
            },
          },
        }))
      },
      setPositions: (mapId, positions) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              positions: [...positions],
            },
          },
        }))
      },
      setEnabled: (mapId, enabled) => {
        set((state) => ({
          measurements: {
            ...state.measurements,
            [mapId]: {
              ...state.measurements[mapId]!,
              enabled,
            },
          },
        }))
      },
    }
  })
}
