import x, { useRef as H, useState as N, useEffect as g } from "react";
import { createEditor as _, createToolbar as k } from "../../editor/dist/index.esm.mjs";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var y = function() {
  return y = Object.assign || function(e) {
    for (var r, o = 1, a = arguments.length; o < a; o++)
      for (var n in r = arguments[o])
        Object.prototype.hasOwnProperty.call(r, n) && (e[n] = r[n]);
    return e;
  }, y.apply(this, arguments);
};
function w(e, r) {
  var o = typeof Symbol == "function" && e[Symbol.iterator];
  if (!o)
    return e;
  var a, n, c = o.call(e), u = [];
  try {
    for (; (r === void 0 || r-- > 0) && !(a = c.next()).done; )
      u.push(a.value);
  } catch (i) {
    n = { error: i };
  } finally {
    try {
      a && !a.done && (o = c.return) && o.call(c);
    } finally {
      if (n)
        throw n.error;
    }
  }
  return u;
}
function B(e) {
  var r = e.defaultContent, o = r === void 0 ? [] : r, a = e.onCreated, n = e.defaultHtml, c = n === void 0 ? "" : n, u = e.value, i = u === void 0 ? "" : u, d = e.onChange, f = e.defaultConfig, s = f === void 0 ? {} : f, h = e.mode, O = h === void 0 ? "default" : h, p = e.style, S = p === void 0 ? {} : p, j = e.className, v = H(null), C = w(N(null), 2), m = C[0], b = C[1], E = w(N(""), 2), D = E[0], T = E[1], A = function(t) {
    a && a(t);
    var l = s.onCreated;
    l && l(t);
  }, P = function(t) {
    T(t.getHtml()), d && d(t);
    var l = s.onChange;
    l && l(t);
  }, R = function(t) {
    var l = s.onDestroyed;
    b(null), l && l(t);
  };
  return g(function() {
    if (m != null && i !== D)
      try {
        m.setHtml(i);
      } catch (t) {
        console.error(t);
      }
  }, [i]), g(function() {
    var t;
    if (v.current != null && m == null && !(!((t = v.current) === null || t === void 0) && t.getAttribute("data-w-e-textarea"))) {
      var l = _({ selector: v.current, config: y(y({}, s), { onCreated: A, onChange: P, onDestroyed: R }), content: o, html: c || i, mode: O });
      b(l);
    }
  }, [m]), x.createElement("div", { style: S, ref: v, className: j });
}
function F(e) {
  var r = e.editor, o = e.defaultConfig, a = o === void 0 ? {} : o, n = e.mode, c = n === void 0 ? "default" : n, u = e.style, i = u === void 0 ? {} : u, d = e.className, f = H(null);
  return g(function() {
    f.current != null && r != null && k({ editor: r, selector: f.current, config: a, mode: c });
  }, [r]), x.createElement("div", { style: i, ref: f, className: d });
}
export {
  B as Editor,
  F as Toolbar
};
