var shared = {
  Host: {
    driver: {}
  }
};
import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import DriverUniversal from 'driver-universal';
import { isStrictByteDanceMicroApp, isStrictWechatMiniProgram } from './env';
var provider = null;
export function onProviderMount(p) {
  provider = p;
}
export function onProviderUnMount() {
  provider = null;
}

function renderWithProvider(element, onHide) {
  return provider.addPortal({
    element: element,
    onHide: onHide
  });
}

function renderWithDriver(element, onHide) {
  if (!shared || !shared.Host || !shared.Host.driver) {
    throw new Error('Render fail: driver not found.');
  }

  var driver = shared.Host.driver;
  var root = document.createElement('div');
  var hid = false;

  var hide = function hide() {
    if (hid) {
      return;
    }

    try {
      hid = true;
      unmountComponentAtNode(root);
      document.body.removeChild(root);

      if (onHide) {
        onHide();
      }
    } catch (e) {
      throw new Error('remove element error');
    }
  };

  if (isStrictWechatMiniProgram || isStrictByteDanceMicroApp) {
    render(element, root);
  } else {
    document.body.appendChild(root);
    render(element, root);
  }

  return hide;
}

export function renderToRoot(element, onHide) {
  if (provider) {
    return renderWithProvider(element, onHide);
  }

  return renderWithDriver(element, onHide);
}