import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useState, useRef, useEffect } from 'react';
import Box from '../box';
import IconButton from '../iconButton';
import Popover from '../popover';
import Label from '../label';
import Input from '../input';
import Button from '../button';
import Tooltip from '../tooltip';
import { TextSmall } from '../typography';
var TooltippedIconButton = Tooltip(IconButton);

var LinkOptions = function LinkOptions(_ref) {
  var _ref$config = _ref.config,
      options = _ref$config.options,
      linkIcon = _ref$config.link.icon,
      defaultTargetOption = _ref$config.defaultTargetOption,
      _ref$currentState = _ref.currentState,
      link = _ref$currentState.link,
      selectionText = _ref$currentState.selectionText,
      onChange = _ref.onChange,
      translations = _ref.translations;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      isPopoverShown = _useState2[0],
      setIsPopoverShown = _useState2[1];

  var iconButtonRef = useRef();

  var _useState3 = useState((link === null || link === void 0 ? void 0 : link.title) || selectionText),
      _useState4 = _slicedToArray(_useState3, 2),
      textValue = _useState4[0],
      setTextValue = _useState4[1];

  var _useState5 = useState(link === null || link === void 0 ? void 0 : link.target),
      _useState6 = _slicedToArray(_useState5, 2),
      linkValue = _useState6[0],
      setLinkValue = _useState6[1];

  useEffect(function () {
    setTextValue((link === null || link === void 0 ? void 0 : link.title) || selectionText);
    setLinkValue(link === null || link === void 0 ? void 0 : link.target);
  }, [link === null || link === void 0 ? void 0 : link.target, selectionText]);

  var focusEditor = function focusEditor() {
    document.querySelector("[aria-label='rdw-editor']").focus();
  };

  var handleOpenPopoverClick = function handleOpenPopoverClick() {
    setIsPopoverShown(true);
  };

  var handleClosePopoverClick = function handleClosePopoverClick() {
    setIsPopoverShown(false);
    focusEditor();
  };

  var onTextChange = function onTextChange(_ref2) {
    var newText = _ref2.currentTarget.value;
    setTextValue(newText);
  };

  var onLinkChange = function onLinkChange(_ref3) {
    var newLink = _ref3.currentTarget.value;
    setLinkValue(newLink);
  };

  var handleAddLinkClick = /*#__PURE__*/function () {
    var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return onChange('link', textValue, linkValue, defaultTargetOption);

            case 2:
              _context.next = 4;
              return setIsPopoverShown(false);

            case 4:
              _context.next = 6;
              return focusEditor();

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function handleAddLinkClick() {
      return _ref4.apply(this, arguments);
    };
  }();

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Box, {
    ref: iconButtonRef
  }, /*#__PURE__*/React.createElement(TooltippedIconButton, {
    "data-wysiwyg": true,
    tooltip: /*#__PURE__*/React.createElement(TextSmall, null, translations['components.controls.link.link']),
    tooltipSize: "small",
    tooltipShowDelay: 1000,
    icon: linkIcon,
    color: "black",
    key: "link",
    onClick: handleOpenPopoverClick,
    selected: !!link || isPopoverShown,
    marginRight: 2
  })), /*#__PURE__*/React.createElement(Popover, {
    anchorEl: iconButtonRef === null || iconButtonRef === void 0 ? void 0 : iconButtonRef.current,
    active: isPopoverShown,
    backdrop: "transparent",
    onEscKeyDown: handleClosePopoverClick,
    onOverlayClick: handleClosePopoverClick,
    minWidth: 276,
    maxWidth: "100%",
    zIndex: 404
  }, /*#__PURE__*/React.createElement(Box, {
    display: "flex",
    flexDirection: "column",
    padding: 4
  }, /*#__PURE__*/React.createElement(Label, {
    htmlFor: "linkText",
    helpText: ""
  }, translations['components.controls.link.popover.text'], /*#__PURE__*/React.createElement(Input, {
    id: "linkText",
    autoFocus: true,
    value: textValue || '',
    onChange: onTextChange,
    "data-wysiwyg": true
  })), /*#__PURE__*/React.createElement(Label, {
    htmlFor: "link",
    helpText: "",
    marginBottom: 4
  }, translations['components.controls.link.popover.link'], /*#__PURE__*/React.createElement(Input, {
    id: "link",
    value: linkValue || '',
    onChange: onLinkChange,
    placeholder: translations['components.controls.link.popover.placeholder'],
    "data-wysiwyg": true
  })), /*#__PURE__*/React.createElement(Box, {
    display: "flex",
    justifyContent: "flex-end",
    alignItems: "center",
    marginTop: 4
  }, /*#__PURE__*/React.createElement(Button, {
    level: "secondary",
    label: translations['components.controls.link.popover.cancel'],
    size: "small",
    marginRight: 3,
    onClick: handleClosePopoverClick,
    "data-wysiwyg": true
  }), /*#__PURE__*/React.createElement(Button, {
    level: "primary",
    label: translations['components.controls.link.popover.addLink'],
    size: "small",
    disabled: !linkValue || !textValue,
    onClick: handleAddLinkClick,
    "data-wysiwyg": true
  })))));
};

export default LinkOptions;