/**
 * pages reducers
 */
import { applicationState } from "../../state"

/**
 * project init
 */
const projectInit = (state, action) => applicationState.compositions

const compositionsAdd = (state, action) => {
  const newState = structuredClone(state)

  action.payload.forEach((composition) => {
    newState[composition._id] = composition
  })

  return newState
}

const compositionsSet = (state, action) => {
  const newState = {}

  action.payload.forEach((composition) => {
    newState[composition._id] = composition
  })

  return newState
}

/* update page */
const compositionsUpdate = (
  state,
  action: {
    payload: {
      compositionId: string
      compositionParams: Record<string, string>
    }
  },
) => {
  const { compositionId, compositionParams } = action.payload
  const newState = structuredClone(state)

  newState[compositionId].data = {
    ...(newState[compositionId].data || {}),
    ...compositionParams,
  }

  return newState
}

export { compositionsAdd, compositionsUpdate, projectInit, compositionsSet }
