import {tolerant_selector} from './base'
import {action_update_workplace} from 'common/workplaces'
import {get_fields_by_workplace} from './fields'
import {map_hash, array_from_hash} from 'common/utilities'

const get_raw_workplaces = (state) => state.missions.workplaces

export const get_workplaces = tolerant_selector(
  [get_raw_workplaces, get_fields_by_workplace],
  (workplaces, fields_by_workplace) => map_hash(workplaces, (workplace) => ({
    ...workplace,
    fields: array_from_hash(fields_by_workplace[workplace.id]),
    update(new_workplace) {
      return action_update_workplace({id: workplace.id, ...new_workplace})
    },
  }))
)
