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 { TableChartOutlined } from '@material-ui/icons';
import React, { useRef, useState } from 'react';
import { useLocalization } from "../../hooks/index.js";
const useStyles = makeStyles(() => {
  return createStyles({
    textField: {
      marginRight: '2rem'
    },
    checkbox: {
      marginTop: '1rem'
    }
  });
});
export const TableControl = _ref => {
  let {
    editor,
    buttonProps
  } = _ref;
  const [open, setOpen] = useState(false);
  const [rows, setRows] = useState();
  const [cols, setCols] = useState();
  const [hasHeader, setHasHeader] = useState(true);
  const classes = useStyles();
  const localization = useLocalization();
  const form = useRef(null);

  const handleClickOpen = () => {
    setOpen(true);
  };

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

  const insertTable = () => {
    if (rows && cols) {
      editor.chain().focus().insertTable({
        rows,
        cols,
        withHeaderRow: hasHeader
      }).run();
    }
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Tooltip, {
    title: localization.menubar.table
  }, /*#__PURE__*/React.createElement(IconButton, _extends({
    onClick: handleClickOpen,
    color: editor.isActive('link') ? 'primary' : 'default'
  }, buttonProps), /*#__PURE__*/React.createElement(TableChartOutlined, null))), /*#__PURE__*/React.createElement(Dialog, {
    open: open,
    onClose: handleClose,
    "aria-labelledby": "table-control-title",
    maxWidth: "sm",
    onExited: () => {
      setRows(undefined);
      setCols(undefined);
      setHasHeader(true);
    }
  }, /*#__PURE__*/React.createElement(DialogTitle, {
    id: "table-control-title"
  }, localization.tableControl.title), /*#__PURE__*/React.createElement(DialogContent, null, /*#__PURE__*/React.createElement("form", {
    ref: form
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(TextField, {
    autoFocus: true,
    margin: "dense",
    label: localization.tableControl.rows,
    type: "number",
    required: true,
    InputProps: {
      inputProps: {
        min: 1
      }
    },
    value: rows !== null && rows !== void 0 ? rows : '',
    onChange: ev => setRows(parseInt(ev.target.value, 10)),
    className: classes.textField
  }), /*#__PURE__*/React.createElement(TextField, {
    margin: "dense",
    label: localization.tableControl.cols,
    type: "number",
    required: true,
    InputProps: {
      inputProps: {
        min: 1
      }
    },
    value: cols !== null && cols !== void 0 ? cols : '',
    onChange: ev => setCols(parseInt(ev.target.value, 10))
  })), /*#__PURE__*/React.createElement(FormControlLabel, {
    control: /*#__PURE__*/React.createElement(Checkbox, {
      checked: hasHeader,
      onChange: event => setHasHeader(event.target.checked),
      color: "primary"
    }),
    label: localization.tableControl.hasHeader,
    className: classes.checkbox
  }))), /*#__PURE__*/React.createElement(DialogActions, null, /*#__PURE__*/React.createElement(Button, {
    onClick: handleClose
  }, localization.common.cancel), /*#__PURE__*/React.createElement(Button, {
    type: "button",
    onClick: () => {
      var _form$current;

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