{"size": 1228, "ext": "js", "max_stars_repo_path": "src/components/landscape-sidebar/geo-description-widget/geo-description-widget-selectors.js", "max_stars_repo_name": "Vizzuality/half-earth-v3", "max_stars_repo_stars_event_min_datetime": "2019-05-23T10:04:44.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-06T10:44:50.000Z", "max_issues_repo_path": "src/components/landscape-sidebar/geo-description-widget/geo-description-widget-selectors.js", "max_issues_repo_name": "Vizzuality/half-earth-v3", "max_issues_repo_issues_event_min_datetime": "2019-04-08T10:17:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T09:31:10.000Z", "max_forks_repo_path": "src/components/landscape-sidebar/geo-description-widget/geo-description-widget-selectors.js", "max_forks_repo_name": "Vizzuality/half-earth-v3", "max_forks_repo_forks_event_min_datetime": "2020-05-01T21:00:16.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-20T20:06:18.000Z"}
{"max_stars_count": 7, "max_issues_count": 356, "max_forks_count": 2, "avg_line_length": 40.9333333333, "max_line_length": 106, "alphanum_fraction": 0.7312703583}
import { createSelector, createStructuredSelector } from 'reselect';

const getGeodescriberData = ({ geoDescription }) => (geoDescription && geoDescription.data) || null;
const getGeodescriberLoading = ({ geoDescription }) => (geoDescription && geoDescription.loading) || null;
const getGeodescriberError = ({ geoDescription }) => (geoDescription && geoDescription.error) || null;

const parseGeodescriberText = (text =  '', params) => {
  const parsedText = Object.keys(params).reduce((acc, key) => {
    return acc.replace(new RegExp(`{${key}}`, 'g'), params[key]);
  }, text);
  return parsedText;
}

const getData = createSelector(
  [getGeodescriberData],
  geodescriberData => {
    if (!geodescriberData) return null;
    const { description, description_params, title, title_params } = geodescriberData;
    const parsedDescription = parseGeodescriberText(description, description_params);
    const parsedTitle = parseGeodescriberText(title, title_params);
    const area = `Area: ${description_params.area_0}`;
    return { description: parsedDescription, title: parsedTitle, area };
  }
);

export default createStructuredSelector({
  data: getData,
  loading: getGeodescriberLoading,
  error: getGeodescriberError
})