import _extends from "@babel/runtime/helpers/extends";
import { Button, Checkbox, createStyles, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, IconButton, makeStyles, TextField, Tooltip } from '@material-ui/core';
import { Link } from '@material-ui/icons';
import React, { useRef, useState } from 'react';
import { useLocalization } from "../../hooks/index.js";
const useStyles = makeStyles(() => {
  return createStyles({
    inNewTab: {
      marginTop: '1rem'
    }
  });
});
export const LinkControl = _ref => {
  let {
    editor,
    buttonProps
  } = _ref;
  const [open, setOpen] = useState(false);
  const [link, setLink] = useState('');
  const [inNewTab, setInNewTab] = useState(false);
  const classes = useStyles();
  const localization = useLocalization();
  const form = useRef(null);

  const handleClickOpen = () => {
    if (editor.state.selection.empty) {
      return false;
    }

    setOpen(true);
    const currentLink = editor.getAttributes('link');

    if (currentLink !== null && currentLink !== void 0 && currentLink.href) {
      setLink(currentLink.href);
      setInNewTab(currentLink.target === '_blank');
    }
  };

  const handleClose = () => {
    setOpen(false);
  };

  const addLink = () => {
    if (link) {
      editor.chain().focus().setLink({
        href: link,
        target: inNewTab ? '_blank' : 'self'
      }).run();
    }
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Tooltip, {
    title: localization.menubar.link
  }, /*#__PURE__*/React.createElement(IconButton, _extends({
    onClick: handleClickOpen,
    color: editor.isActive('link') ? 'primary' : 'default'
  }, buttonProps), /*#__PURE__*/React.createElement(Link, null))), /*#__PURE__*/React.createElement(Dialog, {
    open: open,
    onClose: handleClose,
    "aria-labelledby": "link-control-title",
    fullWidth: true,
    maxWidth: "sm",
    onExited: () => {
      setLink('');
      setInNewTab(false);
    }
  }, /*#__PURE__*/React.createElement(DialogTitle, {
    id: "link-control-title"
  }, localization.linkControl.title), /*#__PURE__*/React.createElement(DialogContent, null, /*#__PURE__*/React.createElement("form", {
    ref: form
  }, /*#__PURE__*/React.createElement(TextField, {
    autoFocus: true,
    margin: "dense",
    label: localization.linkControl.url,
    type: "url",
    required: true,
    fullWidth: true,
    value: link,
    onChange: ev => setLink(ev.target.value)
  }), /*#__PURE__*/React.createElement(FormControlLabel, {
    control: /*#__PURE__*/React.createElement(Checkbox, {
      checked: inNewTab,
      onChange: event => setInNewTab(event.target.checked),
      color: "primary"
    }),
    label: localization.linkControl.openInNewTab,
    className: classes.inNewTab
  }))), /*#__PURE__*/React.createElement(DialogActions, null, /*#__PURE__*/React.createElement(Button, {
    onClick: handleClose
  }, localization.common.cancel), /*#__PURE__*/React.createElement(Button, {
    onClick: () => {
      var _form$current;

      if ((_form$current = form.current) !== null && _form$current !== void 0 && _form$current.reportValidity()) {
        handleClose();
        addLink();
      }
    },
    color: "primary"
  }, localization.linkControl.submit))));
};
//# sourceMappingURL=link-control.js.map