/* eslint-disable no-console */
import baseComponents from './base/index.js';
import React from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
export const components = baseComponents;

/**
 * Registers a react component to be rendered for a given CMS component type.
 *
 * @param type      - Component type. Should be the exact component type in the CMS.
 * @param component - Component to be registered.
 *
 * @returns Ready component.
 */
export const registerComponent = (type, component) => {
  if (components[type]) {
    console.warn(`[react-content] Component with "type=${type}" already registered on components. Will override with new component.`);
  }
  components[type] = component;
  return component;
};

/**
 * Editorial component. Renders a component by looking it up in the registered
 * types.
 *
 * @example
 * ```
 * import { Component } from '@farfetch/blackout-react';
 * ```
 * @example
 * ```
 * <Component
 *      component={{ type, ...data }}
 *      {...props} />
 * ```
 *
 * @param props - All props of component.
 *
 * @returns - A registered component.
 */
const Component = ({
  component,
  children,
  ...props
}) => {
  const {
    type,
    customType
  } = component;
  const typeToRender = customType || type;
  // Lookup a component based on the type
  const ComponentToRender = components[typeToRender];
  if (!ComponentToRender) {
    console.warn(`[react-content] No component with "type=${typeToRender}" is defined.`);
    return null;
  }
  return /*#__PURE__*/_jsx(ComponentToRender, {
    data: component,
    ...props,
    children: children
  });
};
export default Component;