text: const simpleIcons = require('simple-icons');
  
  const ICONS_PER_ROW = 5;
  const ICON_SIZE = 50;
  const ICON_GAP_SIZE = 30;
  const STANDARD_ICON_SIZE = 250;
  const SCALE_RATIO = ICON_SIZE / STANDARD_ICON_SIZE;
  
  const ICON_THEME = Object.freeze({
    dark: '#141414',
    light: '#ffffff',
  });
  
  function generateSvgDom({ title, path, hex, translateX, translateY }) {
    return `
    <g transform="translate(${translateX}, ${translateY})">
      <svg width="${STANDARD_ICON_SIZE}" height="${STANDARD_ICON_SIZE}" viewBox="0 0 24 24" fill="${hex}" xmlns="http://www.w3.org/2000/svg">
        <title>${title}</title>
        <path d="${path}"></path>
      </svg>
    </g>`;
  }
  
  function generateSvgSprite(iconNames, { iconsPerRow, theme }) {
    const iconSvgs = iconNames.reduce((acc, slug) => {
      const icon = simpleIcons.Get(slug);
      if (icon) {
        const { path, hex, title } = icon;
        const fill = acc.push({
          hex: ICON_THEME[theme] ? ICON_THEME[theme] : `#${hex}`,
          path,
          title,
        });
      }
      return acc;
    }, []);
  
    const colCount = Math.min(iconSvgs.length, iconsPerRow);
    const rowCount = Math.ceil(iconSvgs.length / iconsPerRow);
  
    const width = colCount * STANDARD_ICON_SIZE + (colCount - 1) * ICON_GAP_SIZE;
    const height = rowCount * STANDARD_ICON_SIZE + (rowCount - 1) * ICON_GAP_SIZE;
  
    const scaledHeight = height * SCALE_RATIO;
    const scaledWidth = width * SCALE_RATIO;
  
    const svgDoms = iconSvgs.reduce((acc, icon, index) => {
      acc += generateSvgDom({
        ...icon,
        translateX: (index % iconsPerRow) * (STANDARD_ICON_SIZE + ICON_GAP_SIZE),
        translateY: Math.floor(index / iconsPerRow) * (STANDARD_ICON_SIZE + ICON_GAP_SIZE),
      });
      return acc;
    }, '');
  
    return `
      <svg width="${scaledWidth}" height="${scaledHeight}" viewBox="0 0 ${width} ${height}" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
        ${svgDoms}
      </svg>`;
  }
  
  async function handler(event) {
    try {
      // extract icons names from url
      const iconParams = event.queryStringParameters.icons;
      if (!iconParams) {
        throw new Error('No icon is mentioned!', { status: 400 });
      }
      // extract settings from url
      const iconPerRowParams = event.queryStringParameters.row_item || ICONS_PER_ROW;
      if (iconPerRowParams < 1 || iconPerRowParams > 20) {
        throw new Error('Icons per row must be a number between 1 and 20.', {
          status: 400,
        });
      }
  
      const themeParams = event.queryStringParameters.theme || 'colored';
      if (themeParams !== 'colored' && themeParams !== 'dark') {
        throw new Error('Theme must be either color or dark.', {
          status: 400,
        });
      }
  
      // generate svgs
      const svg = generateSvgSprite(iconParams.split(','), {
        iconsPerRow: iconPerRowParams,
        theme: themeParams,
      });
  
      return {
        statusCode: 200,
        headers: {
          'Content-Type': 'image/svg+xml',
        },
        body: svg,
      };
    } catch (error) {
      return { statusCode: 500, body: error.toString() };
    }
  }
  
  module.exports = { handler };
  