import fullNames from '@/modules/designSystem/modules/DataGridSection/internal/fullNamesList.json';

export const colorList = [
  'primary',
  'secondary',
  'info',
  'error',
  'warning',
  'highlight',
  'highlight2',
  'complimentary',
  'success',
  'neutral',
];

export const shapes = ['square', 'rounded', 'circular'];

export const imageAvatarUrl =
  'data:image/jpeg;base64,[encoded data, 5212 characters]';

const rawStringDetectReg = /['"]!!!(.*?)['"]/g;

export const getPropsString = (reactProps, options) => {
  const { spaceCount } = {
    ...(options || {}),
    spaceCount: 2,
  };

  const result = Object.entries(reactProps)
    .filter(([key, value]) => value !== undefined && !/^\$\$/.test(key))
    .map(([key, value]) => {
      let isStringQuote = false;

      if (typeof value === 'boolean') {
        if (value) {
          return key;
        }

        return null;
      }

      let value$ = value;

      if (typeof value === 'function') {
        value$ = `${key}Fn`;
      } else if (typeof value === 'object') {
        value$ = JSON.stringify(value, null, 2).replace(rawStringDetectReg, '$1');
      }

      if (typeof value === 'string') {
        if (/!!!/.test(value)) {
          value$ = value.replace(rawStringDetectReg, '$1').replace(/!!!/, '');
        } else {
          isStringQuote = true;
        }
      }

      if (typeof value === 'string' && /\/\*\*\//.test(value)) {
        return `\n// ${key}={${value$}}\n`;
      }

      const resValue = isStringQuote ? `"${value$}"` : `{${value$}}`;

      return `${key}=${resValue}`;
    })
    .filter(Boolean);

  let string = '';

  if (result.length) {
    string = ' ';
    const oneLine = result.join(' ');

    if (oneLine.length > 60) {
      string += `\n${' '.repeat(spaceCount)}`;
      string += result.join(`\n${' '.repeat(spaceCount)}`);
      string += '\n';
    } else {
      string += oneLine;
    }
  }

  return string;
};

const getImportString = (listNames, fromString) => {
  let list = null;

  if (listNames.length) {
    const fixedList = [...new Set(listNames)].filter(Boolean);
    const divider = fixedList.length <= 3 ? ' ' : '\n';

    list = `${divider}${fixedList.join(`,${divider}`)}${divider}`;
  }

  return list ? `import {${list}} from '${fromString}';` : '';
};

const defaultFrom = '@/mui/components';

export const getImportsString = (listNames) => {
  const dataString = listNames
    .filter(Boolean)
    .reduce((acc, [list, from]) => {
      const from$ = from || defaultFrom;
      const index = acc.findIndex((item) => item[1] === from$);

      if (index === -1) {
        acc.push([list, from$]);
      } else {
        acc[index][0] = [...acc[index][0], ...list];
      }

      return acc;
    }, [])
    .sort((a, b) => a[1].localeCompare(b[1]))
    .map(([list, from]) => getImportString(list, from))
    .join('\n');

  return `${dataString}\n`;
};

export const getKeyByState = (state) => Object.entries(state).reduce((acc, item) => `${acc}${item}`, '');

export const getOptions = (count) =>
  new Array(count).fill(0).map((_, index) => ({
    label: `${fullNames.firstName[index]} ${fullNames.lastName[index]}`,
    value: index,
  }));

export default {};
