"use strict";

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

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

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

var _reactCopyToClipboard = require("react-copy-to-clipboard");

var _Button = _interopRequireDefault(require("../Button"));

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

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}
 * @private
 */
var CopyButton = function CopyButton(_ref) {
  var className = _ref.className,
      onCopy = _ref.onCopy,
      style = _ref.style,
      testSection = _ref.testSection,
      textLabel = _ref.textLabel,
      textToCopy = _ref.textToCopy,
      usesTextLabel = _ref.usesTextLabel,
      props = _objectWithoutProperties(_ref, ["className", "onCopy", "style", "testSection", "textLabel", "textToCopy", "usesTextLabel"]);

  return _react["default"].createElement(_reactCopyToClipboard.CopyToClipboard, {
    text: textToCopy,
    onCopy: onCopy
  }, _react["default"].createElement(_Button["default"], _extends({
    className: className,
    style: style === "none" ? null : style,
    size: usesTextLabel ? "tiny" : null,
    ariaLabel: "Copy code snippet",
    testSection: testSection ? "".concat(testSection, "-copy-button") : null
  }, props), usesTextLabel ? textLabel : _react["default"].createElement(_reactOuiIcons["default"], {
    name: "clipboard"
  })));
};

CopyButton.propTypes = {
  /** CSS class names. */
  className: _propTypes["default"].string,

  /** Function to perform when content is copied.
    This callback is passed as onCopy prop to CopyToClipboard component.
    onCopy(text, result)
    result (bool): Returns true if copied successfully, else false.
  */
  onCopy: _propTypes["default"].func,

  /** Style option for the button */
  style: _propTypes["default"].oneOf(["highlight", "danger", "danger-outline", "outline", "outline-reverse", "plain", "toggle", "underline", "unstyled", "none"]),

  /** Hook for automated JavaScript tests */
  testSection: _propTypes["default"].string,

  /** String label to use when usesTextLabel is true */
  textLabel: _propTypes["default"].string,

  /** The text or code that will be copied */
  textToCopy: _propTypes["default"].string.isRequired,

  /**
   * Whether or not this should use a string label
   * instead of an icon
   */
  usesTextLabel: _propTypes["default"].bool
};
CopyButton.defaultProps = {
  style: "plain",
  usesTextLabel: false,
  textLabel: "Copy"
};
var _default = CopyButton;
exports["default"] = _default;