import { createSelector } from "reselect";
import {
    FETCH_COMPANIES_SUCCESS,
    FETCH_COMPANIES_FAILURE,
    FETCH_COMPANIES_REQUEST,
} from "../types";

export function companies(state = {
}, action = {}) {

    switch (action.type) {
        case FETCH_COMPANIES_REQUEST:
            return {
                ...state,
                loading: true
            };
        case FETCH_COMPANIES_SUCCESS:
            return {
                ...state,
                ...action.res,
                loading:false
            };
        case FETCH_COMPANIES_FAILURE:
            return {
                ...state,
                errors: action.error,
                loading:false
            };

        default:
            return state;
    }
}

export const CompaniesHashSelector = state => state.companies.data;

export const CompaniesSelector = createSelector(CompaniesHashSelector, hash =>
    Object.values(hash)
);
