import React from 'react';
import { imagePrettyUrl } from './getters';

export function capitalize(string) {
  return string.replace(/^\w/, (c) => c.toUpperCase());
}

export function anchorLink(string) {
  return string.replace(/\s/g, '-').toLowerCase();
}

// export function getLinkListFromPath(path) {
//   if (path === '/') return [];

//   let links = path?.split('/');

//   links = links.map((link) => {
//     const linkLabel = link;
//     const label = linkLabel.charAt(0).toUpperCase() + linkLabel.slice(1);

//     const linkFormat = {
//       path: `/${link}`,
//       label,
//     };

//     return linkFormat;
//   });

//   return links;
// }

export function leftTrim(str) {
  if (!str) return str;
  // remove space from begining
  const trim = str.replace(/^\s+/g, '');
  // remove multiple spaces
  const output = trim.replace(/ +(?= )/g, '');
  return output;
}

export function truncateString(str, num, dots = '...') {
  if (!str || str.length <= num) {
    return str;
  }
  let trimmedString = str.substr(0, num);

  if (str.charAt(num) === ' ') {
    return `${trimmedString}${dots}`;
  }
  const hasSpace = trimmedString.lastIndexOf(' ') !== -1;
  if (!hasSpace) {
    return `${trimmedString}${dots}`;
  }
  trimmedString = trimmedString.substr(0, trimmedString.lastIndexOf(' '));
  return `${trimmedString}${dots}`;
}

export function cleanHTML(string) {
  return string && string.replace(/(<([^>]+)>)/gi, '');
}

export function parseContentImageUrl(src) {
  const srcArr = src && src.split('//');
  if (srcArr && srcArr.length === 2) {
    const refUrl = srcArr[1].substring(srcArr[1].indexOf('/'));

    return imagePrettyUrl(refUrl.substring(1));
  }

  return src;
}

export const removeSymbols = (string) => string.replace(/[!?@Â£#$%^&*():;"'|/.,~`]/g, '');

export function replaceWith(string, replaceThis, replacement) {
  return string
    .split(replaceThis)
    .map((item) => <>{item}</>)
    .reduce((acc, x) => (acc === null ? [x] : [acc, replacement, x]), null);
}
