import { showPanel } from '@codemirror/panel';
import { StateEffect } from '@codemirror/state';
export const updateErrors = StateEffect.define();
const renderErrors = (errors = []) => errors
  .map((error) => {
  var _a, _b;
  if (typeof error === 'string') {
    return `<stencila-code-error kind="warning">${error}</stencila-code-error>`;
  }
  return `<stencila-code-error kind=${(_a = error.errorType) !== null && _a !== void 0 ? _a : 'info'} has-stacktrace="${(error.stackTrace !== undefined).toString()}">${error.errorMessage}<pre slot="stacktrace">${(_b = error.stackTrace) !== null && _b !== void 0 ? _b : ''}</pre></stencila-code-error>`;
})
  .join('\n');
const errorPanel = () => {
  const dom = document.createElement('div');
  dom.innerHTML = renderErrors();
  return {
    dom,
    update(update) {
      for (const transaction of update.transactions) {
        for (const effect of transaction.effects) {
          if (effect.is(updateErrors)) {
            dom.innerHTML = renderErrors(effect.value);
          }
        }
      }
    },
  };
};
export const codeErrors = () => showPanel.of(errorPanel);
