"use strict";
var import_IpynbRenderer = require("../../IpynbRenderer.css");
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const kit_utils_error = require("../utils/error.cjs");
const NotebookJS = require("notebook");
(function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
});
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
  switch (arguments.length) {
    case 1:
      return a;
    case 2:
      return ab(a);
    case 3:
      return bc(ab(a));
    case 4:
      return cd(bc(ab(a)));
    case 5:
      return de(cd(bc(ab(a))));
    case 6:
      return ef(de(cd(bc(ab(a)))));
    case 7:
      return fg(ef(de(cd(bc(ab(a))))));
    case 8:
      return gh(fg(ef(de(cd(bc(ab(a)))))));
    case 9:
      return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
    default: {
      var ret = arguments[0];
      for (var i = 1; i < arguments.length; i++) {
        ret = arguments[i](ret);
      }
      return ret;
    }
  }
}
(function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
});
var isLeft$1 = function(ma) {
  return ma._tag === "Left";
};
var left$1 = function(e) {
  return { _tag: "Left", left: e };
};
var right$1 = function(a) {
  return { _tag: "Right", right: a };
};
var left = left$1;
var right = right$1;
var mapLeft = function(f) {
  return function(fa) {
    return isLeft(fa) ? left(f(fa.left)) : fa;
  };
};
var isLeft = isLeft$1;
var matchW = function(onLeft, onRight) {
  return function(ma) {
    return isLeft(ma) ? onLeft(ma.left) : onRight(ma.right);
  };
};
var match = matchW;
var tryCatch = function(f, onThrow) {
  try {
    return right(f());
  } catch (e) {
    return left(onThrow(e));
  }
};
const parseNotebook = (file) => {
  const json = JSON.parse(file);
  const notebookJS = NotebookJS.parse(json);
  return notebookJS.render().outerHTML;
};
const JupyterRenderer = React.memo(({ file, onError }) => {
  const parseResult = React.useMemo(() => {
    return tryCatch(
      () => parseNotebook(file),
      (e) => e
    );
  }, [file]);
  React.useEffect(() => {
    pipe(
      parseResult,
      mapLeft((e) => {
        onError(e, {
          publicMessage: "Failed to load selected notebook.",
          publicSubject: "Unable to parse the selected notebook.",
          silent: true,
          type: kit_utils_error.ErrorType.Input
        });
      })
    );
  }, [parseResult, onError]);
  return pipe(
    parseResult,
    match(
      () => /* @__PURE__ */ jsxRuntime.jsx("div", { children: file }),
      (__html) => /* @__PURE__ */ jsxRuntime.jsx("div", { className: "ipynb-renderer-root", dangerouslySetInnerHTML: { __html } })
    )
  );
});
exports.default = JupyterRenderer;
exports.parseNotebook = parseNotebook;
