import { DEVICE_OPTIONS } from '../constants';

function getDevice(device = []) {
  return device.reduce((acc, item) => {
    const deviceValue = DEVICE_OPTIONS.find((option) => option.value === item);
    if (deviceValue) {
      acc.push(deviceValue);
    }
    return acc;
  }, []);
}

function getGeo(geo = [], countryOptions) {
  return !geo.length ? countryOptions : geo.map((item) => countryOptions.find((option) => option.value === item));
}

function responseToTableRow({ countryOptions }) {
  return (row) => {
    const device = getDevice(row.device);
    const geo = getGeo(row.geo?.geo, countryOptions);

    return {
      id: row.uid,
      startDateTime: row.startDateTime,
      endDateTime: row.endDateTime,
      status: row.status,
      device,
      geo,
      geoAction: row.geo.action,
      viewsBudget: row.viewsBudget,
      viewsDelivered: row.viewsDelivered || 0,
      pacing: row.pacing,
    };
  };
}

export default responseToTableRow;
