"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextMergeView = TextMergeView;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _merge = require("@codemirror/merge");
var _state = require("@codemirror/state");
var _view = require("@codemirror/view");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const readOnlyExtensions = [
// only read-only is supported
_view.EditorView.editable.of(false), _state.EditorState.readOnly.of(true)];
const viewSettingsDefault = {
  showLineNumbers: true,
  highlightActiveLine: false
};

/**
 * Merge view for 2 text versions
 * Limited to read-only view.
 * @midnight-gray-supported
 * @alpine-light-supported
 */
function TextMergeView({
  className,
  testId = 'text-merge-view-container',
  isDarkTheme = true,
  values,
  viewSettings = viewSettingsDefault
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const mergeViewParentRef = (0, _react.useRef)(null);
  const mergeViewRef = (0, _react.useRef)(null);
  const ariaLabelForSection = t('Text merge view - read only');
  const allExtensionsMemo = (0, _react.useMemo)(() => {
    const extentions = [...readOnlyExtensions, _view.EditorView.contentAttributes.of({
      'aria-label': ariaLabelForSection
    })];
    if (viewSettings.showLineNumbers) {
      extentions.push((0, _view.lineNumbers)());
    }
    if (viewSettings.highlightActiveLine) {
      extentions.push((0, _view.highlightActiveLine)());
    }
    extentions.push(_view.EditorView.theme({}, {
      dark: isDarkTheme
    }));
    return extentions;
  }, [viewSettings, isDarkTheme, ariaLabelForSection]);
  (0, _react.useEffect)(() => {
    return () => {
      mergeViewRef.current?.destroy();
      mergeViewRef.current = null;
    };
  }, []);
  (0, _react.useEffect)(() => {
    mergeViewRef.current = new _merge.MergeView({
      a: {
        doc: values.left,
        extensions: [...allExtensionsMemo]
      },
      b: {
        doc: values.right,
        extensions: [...allExtensionsMemo]
      },
      // @ts-expect-error because parent: Element | DocumentFragment | undefined allows undefined and ref does not allow
      parent: mergeViewParentRef.current
    });
  }, [allExtensionsMemo]);
  (0, _react.useEffect)(() => {
    const mv = mergeViewRef.current;
    if (!mv || !values) {
      return;
    }
    const replaceDoc = (view, text) => {
      const current = view.state.doc.toString();
      if (current === text) {
        return;
      }
      view.dispatch({
        changes: {
          from: 0,
          to: view.state.doc.length,
          insert: text
        }
      });
    };
    // Update only if content changed
    replaceDoc(mv.a, values.left);
    replaceDoc(mv.b, values.right);
  }, [values]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)('text-merge-view', className),
    "test-id": testId,
    ref: mergeViewParentRef,
    "aria-label": t('Comparison of two text view'),
    "aria-readonly": true,
    tabIndex: 0,
    role: "textbox"
  });
}