"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DiffItem = exports.DiffMethodLink = void 0;

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

var _reactRouter = require("react-router");

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _components = require("@looker/components");

var _reactRedux = require("react-redux");

var _state = require("../../../state");

var _utils = require("../../../utils");

var _DiffBanner = require("./DiffBanner");

var _docDiffUtils = require("./docDiffUtils");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var DiffLink = (0, _styledComponents.default)(_components.Link).withConfig({
  displayName: "DiffItem__DiffLink",
  componentId: "sc-1laajiy-0"
})(["color:", ";cursor:pointer;display:block;padding:", ";&:hover,&:visited,&:focus,&.active{color:", ";cursor:pointer;}"], _ref => {
  var {
    theme
  } = _ref;
  return theme.colors.ui5;
}, _ref2 => {
  var {
    theme
  } = _ref2;
  return "".concat(theme.space.xsmall, " ").concat(theme.space.large);
}, _ref3 => {
  var {
    theme
  } = _ref3;
  return theme.colors.key;
});

var DiffMethodLink = _ref4 => {
  var {
    method,
    specKey
  } = _ref4;
  var history = (0, _reactRouter.useHistory)();
  if (!method) return _react.default.createElement(_components.Heading, {
    as: "h4"
  }, "Missing in ".concat(specKey));
  var tag = method.schema.tags[0];
  var path = "".concat((0, _utils.buildMethodPath)(specKey, tag, method.name));
  return _react.default.createElement(DiffLink, {
    role: "link",
    onClick: () => {
      history.push(path);
    }
  }, "".concat(method.name, " for ").concat(specKey));
};

exports.DiffMethodLink = DiffMethodLink;

var DiffItem = _ref5 => {
  var {
    item,
    leftKey,
    leftSpec,
    rightKey,
    rightSpec
  } = _ref5;
  var selectedSdkLanguage = (0, _reactRedux.useSelector)(_state.selectSdkLanguage);
  var [leftMethod, setLeftMethod] = (0, _react.useState)(leftSpec.methods[item.name]);
  var [rightMethod, setRightMethod] = (0, _react.useState)(rightSpec.methods[item.name]);
  var [method, setMethod] = (0, _react.useState)(leftMethod || rightMethod);
  var [isOpen, setIsOpen] = (0, _react.useState)(false);
  var [leftSide, setLeftSide] = (0, _react.useState)('');
  var [rightSide, setRightSide] = (0, _react.useState)('');
  (0, _react.useEffect)(() => {
    var {
      lhs,
      rhs
    } = (0, _docDiffUtils.differ)(item, leftSpec, rightSpec, selectedSdkLanguage);
    var lMethod = leftSpec.methods[item.name];
    var rMethod = rightSpec.methods[item.name];
    setLeftMethod(lMethod);
    setRightMethod(rMethod);
    setMethod(lMethod || rMethod);
    setLeftSide(lhs);
    setRightSide(rhs);
  }, [leftSpec, rightSpec, isOpen, selectedSdkLanguage]);

  var handleOpen = () => {
    setIsOpen(!isOpen);
  };

  return _react.default.createElement(_components.Card, {
    border: true,
    width: "100%"
  }, _react.default.createElement(_components.Accordion2, {
    indicatorPosition: "left",
    isOpen: isOpen,
    toggleOpen: handleOpen,
    label: _react.default.createElement(_DiffBanner.DiffBanner, {
      item: item,
      method: method
    })
  }, _react.default.createElement(_components.Grid, {
    columns: 2
  }, _react.default.createElement(_components.Box, null, _react.default.createElement(DiffMethodLink, {
    method: leftMethod,
    specKey: leftKey
  })), _react.default.createElement(_components.Box, null, _react.default.createElement(DiffMethodLink, {
    method: rightMethod,
    specKey: rightKey
  }))), _react.default.createElement(_reactDiffViewer.default, {
    oldValue: leftSide,
    newValue: rightSide,
    splitView: true,
    compareMethod: _reactDiffViewer.DiffMethod.LINES,
    showDiffOnly: true
  })));
};

exports.DiffItem = DiffItem;
//# sourceMappingURL=DiffItem.js.map