import React, { useState } from 'react';
import { Button } from '@grafana/ui';
import { LoadingState } from '@grafana/data';

const RunQueryButtons = (props) => {
  const { state } = props;
  const [running, setRunning] = useState(false);
  const [stopping, setStopping] = useState(false);
  const [prevState, setPrevState] = useState(state);
  const [lastQuery, setLastQuery] = useState(props.query);
  if (state !== prevState) {
    setPrevState(state);
    if (state && state !== LoadingState.Loading) {
      setRunning(false);
      setStopping(false);
    }
  }
  const onRunQuery = () => {
    setRunning(true);
    setLastQuery(props.query);
    props.onRunQuery();
  };
  const onCancelQuery = props.onCancelQuery ? () => {
    var _a;
    (_a = props.onCancelQuery) == null ? undefined : _a.call(props, lastQuery);
    setStopping(true);
  } : undefined;
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
    Button,
    {
      variant: props.enableRun ? "primary" : "secondary",
      size: "sm",
      onClick: onRunQuery,
      icon: running && !stopping ? "fa fa-spinner" : undefined,
      disabled: state === LoadingState.Loading || !props.enableRun
    },
    "Run query"
  ), /* @__PURE__ */ React.createElement(
    Button,
    {
      variant: running && !stopping ? "primary" : "secondary",
      size: "sm",
      disabled: !running || stopping,
      icon: stopping ? "fa fa-spinner" : undefined,
      onClick: onCancelQuery
    },
    "Stop query"
  ));
};

export { RunQueryButtons };
//# sourceMappingURL=RunQueryButtons.js.map
