"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.CodeDiff = void 0;

var _classnames = _interopRequireDefault(require("classnames"));

var _lodash = require("lodash");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireDefault(require("react"));

var _reactOuiIcons = _interopRequireDefault(require("react-oui-icons"));

var _reactDiffViewer = _interopRequireDefault(require("react-diff-viewer"));

var _index = require("../../tokens/forimport/index.es");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/**
 *
 * @param {Object} props - Properties passed to component
 * @returns {ReactElement}
 */
var CodeDiff = function CodeDiff(_ref) {
  var borderLocation = _ref.borderLocation,
      className = _ref.className,
      header = _ref.header,
      newValue = _ref.newValue,
      oldValue = _ref.oldValue,
      testSection = _ref.testSection,
      props = _objectWithoutProperties(_ref, ["borderLocation", "className", "header", "newValue", "oldValue", "testSection"]);

  var before = (0, _lodash.cloneDeep)(oldValue);
  var after = (0, _lodash.cloneDeep)(newValue); // Attempt to stringify, since non-strings will
  // throw an error in ReactDiffViewer anyways

  if ([before, after].some(function (val) {
    return typeof val !== "string";
  })) {
    before = JSON.stringify(before, null, 2);
    after = JSON.stringify(after, null, 2);
  }

  return _react["default"].createElement("div", {
    className: (0, _classnames["default"])("oui-codediff", {
      "border--top": borderLocation.includes("top"),
      "border--right": borderLocation.includes("right"),
      "border--bottom": borderLocation.includes("bottom"),
      "border--left": borderLocation.includes("left")
    }, className),
    "data-test-section": testSection
  }, header && _react["default"].createElement("div", {
    className: "background--grey-25 border--bottom color--base soft--sides soft-half--ends"
  }, header), _react["default"].createElement(_reactDiffViewer["default"], _extends({}, props, {
    codeFoldMessageRenderer: function codeFoldMessageRenderer() {
      return (// eslint-disable-line react/jsx-no-bind
        _react["default"].createElement("div", {
          className: "flex",
          style: {
            marginTop: 3
          }
        }, _react["default"].createElement(_reactOuiIcons["default"], {
          name: "ellipsis",
          fill: _index.brandBlueDark
        }))
      );
    },
    oldValue: before,
    newValue: after
    /**
     * BEGIN: TODO(AXIOM-138) Ensure styling is correct and remove
     * remaining default declarations mentioned below
     */
    // See https://github.com/praneshr/react-diff-viewer/tree/v3.0.0#overriding-styles
    // for more info on styling options
    ,
    styles: {
      line: {
        wordBreak: "break-word"
      },
      codeFoldGutter: {
        backgroundColor: "#f1f8ff" // Sames as variables.light.codeFoldBackground

      }
    }
    /**
     * END: TODO(AXIOM-138) Ensure styling is correct and
     * remove remaining default declarations above
     */

  })));
};

exports.CodeDiff = CodeDiff;
CodeDiff.propTypes = {
  /** Where borders should be added. Defaults to borders on every side (OUI) */
  borderLocation: _propTypes["default"].arrayOf(_propTypes["default"].oneOf(["top", "right", "bottom", "left"])),

  /** CSS class names. */
  className: _propTypes["default"].string,

  /**
   * Differential title that spans all columns.
   * If not provided, no header will be shown (OUI)
   */
  header: _propTypes["default"].node,

  /**
   * New value. If of type "object", will be
   * formatted with JSON.stringify (ReactDiffViewer)
   */
  newValue: _propTypes["default"].any.isRequired,

  /** Show and hide line numbers (ReactDiffViewer) */
  oldValue: _propTypes["default"].any.isRequired,

  /** Value used for testing via data-test-section attribute (OUI) */
  testSection: _propTypes["default"].string
  /**
   * Get info on the core component (ReactDiffViewer)
   * props at https://github.com/praneshr/react-diff-viewer/tree/v3.0.0#props
   */

};
CodeDiff.defaultProps = {
  borderLocation: ["top", "right", "bottom", "left"],
  compareMethod: "diffWords",
  header: "",
  testSection: ""
};
var _default = CodeDiff;
exports["default"] = _default;