import React from "react";
import ReactDOM from "react-dom";
import App, { Editor } from "./App";
// import registerServiceWorker from "./registerServiceWorker";

const Index = props => (
  <App showAlertComment showAlertReply showAlertFavor showError {...props}>
    <Editor maxUpload={9} autoFocus />
  </App>
);

/**
 * 渲染评论组件
 * @param {object} config 编辑器配置
 *  - {string} id 渲染评论的DOM的 ID
 *  - {number} type 评论的 type
 *  - {string} businessId 评论的 businessId
 *  - {string} API, API 前缀, 默认 http://api.links123.net/comment/v1
 */
function renderComment(config) {
  if (!config.id) {
    throw new Error("id is required");
  }
  if (!config.type) {
    throw new Error("type is required");
  }
  if (!config.businessId) {
    // throw new Error("businessId is required");
    console.warn("没有传入 businessId 参数，默认使用: test");
  }
  if (!config.API) {
    // throw new Error("API is required");
    console.warn(
      "没有传入 API 参数，默认使用: http://api.links123.net/comment/v1"
    );
  }
  const {
    id,
    type = 1,
    businessId = "test",
    API = "http://api.links123.net/comment/v1",
    userId
  } = config;

  ReactDOM.render(
    <Index type={type} businessId={businessId} API={API} userId={userId} />,
    document.getElementById(id)
  );
  // registerServiceWorker();
}

window.renderComment = renderComment;

renderComment({
  id: "root-comment",
  type: 1,
  businessId: "test"
});

// renderComment({
//   id: "root-comment",
//   type: 1,
//   businessId: "test",
//   API: 'http://api.links123.net/comment/v1',
// });
