/**
 * project reducer
 */
import { applicationState } from "../../state"
import { clone, mergeDeepRight } from "ramda"
import reducerFromMap from "@racletteCore/lib/reducerFromMap"
const removeEmptyAttributes = (
  obj: Record<string, any>,
): Record<string, any> => {
  // Create a new object to store non-empty attributes
  const result: Record<string, any> = {}

  // Check each key in the object
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      const value = obj[key]

      // Check if value is null or undefined
      if (value === null || value === undefined) {
        continue
      }

      // Check if value is an empty string
      if (typeof value === "string" && value.trim() === "") {
        continue
      }

      // Check if value is an array and is empty
      if (Array.isArray(value) && value.length === 0) {
        continue
      }

      // Check if value is an object
      if (typeof value === "object" && !Array.isArray(value)) {
        // Recursively remove empty attributes from nested objects
        const cleanedObject = removeEmptyAttributes(value)

        // Only include the object if it has keys after cleaning
        if (Object.keys(cleanedObject).length > 0) {
          result[key] = cleanedObject
        }
      } else {
        // Keep the attribute if it passed all the emptiness checks
        result[key] = value
      }
    }
  }

  return result
}
// map instead of switch statement
const reducerMap = {
  init: () => applicationState.project,
  projectInit: (state, action) => {
    const newState = clone(state)

    delete newState.queryModifier

    return newState
  },
  "project/updateConfig": (state, action) => {
    const config = action.payload
    const configType = action.configType

    if (!config || !configType) {
      return state
    }

    if (state._id !== config._project) {
      return state
    }
    action.actionResult.updated.itemsBefore[config._id] = clone(
      state.configs[configType],
    )
    const newState = clone(state)

    newState.configs[configType] = config
    action.actionResult.updated.items[config._id] = config

    return newState
  },
  "project/addConfig": (state, action) => {
    const config = action.payload

    if (!config) {
      return state
    }

    if (state._id !== config._project) {
      return state
    }
    const newState = {
      ...state,
      configs: {
        ...(state.configs || {}),
        [config._type]: config._source,
      },
    }

    // Perform deep merge of all configs
    newState.config = Object.values(newState.configs).reduce(
      (mergedConfig, configObj) =>
        mergeDeepRight(mergedConfig, removeEmptyAttributes(configObj.config)),

      {},
    )

    // Update action result
    action.actionResult.added.push(config)

    return newState
  },
  "project/addQueryModifier": (state, action) => ({
    ...state,
    ...{ queryModifier: action.payload },
  }),
  "project/setId": (state, action) => {
    const newState = clone(state)

    newState._id = action.payload

    return newState
  },
  "project/set": (state, action) => action.payload,
}

const projectReducers = reducerFromMap(reducerMap)

export { projectReducers }
