{"size": 2740, "ext": "js", "max_stars_repo_path": "src/reducers/paths.js", "max_stars_repo_name": "anthonydugois/svg-path-builder", "max_stars_repo_stars_event_min_datetime": "2015-10-26T18:58:57.000Z", "max_stars_repo_stars_event_max_datetime": "2016-04-09T16:28:57.000Z", "max_issues_repo_path": "src/reducers/paths.js", "max_issues_repo_name": "anthonydugois/polynom", "max_issues_repo_issues_event_min_datetime": "2016-04-12T23:53:10.000Z", "max_issues_repo_issues_event_max_datetime": "2018-05-06T06:34:03.000Z", "max_forks_repo_path": "src/reducers/paths.js", "max_forks_repo_name": "anthonydugois/svg-path-builder", "max_forks_repo_forks_event_min_datetime": "2015-10-27T01:34:26.000Z", "max_forks_repo_forks_event_max_datetime": "2016-01-19T06:46:06.000Z"}
{"max_stars_count": 248, "max_issues_count": 23, "max_forks_count": 17, "avg_line_length": 20.6015037594, "max_line_length": 73, "alphanum_fraction": 0.5989051095}
import * as ActionTypes from "../constants/ActionTypes"

const initialState = {
  0: {
    id: 0,
    name: "Default",
    isActive: true,
    isClosed: false,
    isRelative: false,
    isFilled: false,
    isBordered: true,
    points: [0],
  },
}

const path = (state, action) => {
  switch (action.type) {
  case ActionTypes.ADD_PATH:
    return {
      id: action.pathId,
      name: `Path ${ action.pathId }`,
      isActive: false,
      isClosed: false,
      isRelative: false,
      isFilled: false,
      isBordered: true,
      points: [],
    }

  case ActionTypes.SET_PATH_NAME:
    return {
      ...state,
      name: action.name,
    }

  case ActionTypes.SET_ACTIVE_PATHS:
    return {
      ...state,
      isActive: action.isActive,
    }

  case ActionTypes.SET_RELATIVE_PATH:
    return {
      ...state,
      isRelative: action.isRelative,
    }

  case ActionTypes.SET_CLOSED_PATH:
    return {
      ...state,
      isClosed: action.isClosed,
    }

  case ActionTypes.SET_FILLED_PATH:
    return {
      ...state,
      isFilled: action.isFilled,
    }

  case ActionTypes.SET_BORDERED_PATH:
    return {
      ...state,
      isBordered: action.isBordered,
    }

  case ActionTypes.INSERT_POINT:
    return {
      ...state,
      points: [
        ...state.points.slice(0, action.insertAt),
        action.pointId,
        ...state.points.slice(action.insertAt),
      ],
    }

  case ActionTypes.REMOVE_POINTS:
    return {
      ...state,
      points: state.points.filter((id) => !action.pointIds.includes(id)),
    }

  default:
    return state
  }
}

export default (state = initialState, action) => {
  switch (action.type) {
  case ActionTypes.ADD_PATH:
    return {
      ...state,
      [action.pathId]: path(undefined, action),
    }

  case ActionTypes.DELETE_PATHS:
    return Object.keys(state).reduce(
      (acc, key) => (action.pathIds.includes(state[key].id) ? acc : {
        ...acc,
        [key]: state[key],
      }),
      {}
    )

  case ActionTypes.SET_ACTIVE_PATHS:
    return Object.keys(state).reduce((acc, key) => ({
      ...acc,
      [key]: action.pathIds.includes(state[key].id) ?
        path(state[key], action) : state[key],
    }), {})

  case ActionTypes.REMOVE_POINTS:
    return Object.keys(state).reduce((acc, key) => ({
      ...acc,
      [key]: path(state[key], action),
    }), {})

  case ActionTypes.SET_PATH_NAME:
  case ActionTypes.SET_ACTIVE_PATH:
  case ActionTypes.SET_RELATIVE_PATH:
  case ActionTypes.SET_CLOSED_PATH:
  case ActionTypes.SET_FILLED_PATH:
  case ActionTypes.SET_BORDERED_PATH:
  case ActionTypes.INSERT_POINT:
    return {
      ...state,
      [action.pathId]: path(state[action.pathId], action),
    }

  default:
    return state
  }
}
