import React from 'react';
import CapLabel from '@capillarytech/cap-ui-library/CapLabel';

/**
 * Builds a JSX description for template archive/unarchive notifications.
 * @param {string} labelText - Localised label (e.g. "Template name ")
 * @param {string} templateName - The template name to display
 */
export const buildTemplateNameDescription = (labelText, templateName) => (
  templateName
    ? (
      <span>
        <CapLabel.CapLabelInline type="label1" className="notification-template-label">{labelText}</CapLabel.CapLabelInline>
        <CapLabel.CapLabelInline type="label1" className="notification-template-name">{templateName}</CapLabel.CapLabelInline>
      </span>
    )
    : undefined
);

// returns query items as obj when query string is passed
/**
 *
 * @param {*} searchString // '?channel=whatsapp&mode=create
 * @returns // { channel: 'whatsapp', mode: 'create' }
 */
export const getObjFromQueryParams = (searchString) => {
    const params = new URLSearchParams(searchString);
    const obj = {};
    // iterate over all keys
    for (const key of params.keys()) {
        if (params.getAll(key).length > 1) {
            obj[key] = params.getAll(key);
        } else {
            obj[key] = params.get(key);
        }
    }
    return obj;
}