import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "value", "placeholder", "className", "contentProps", "textProps", "compressed", "isDisabled", "isInvalid", "fullWidth", "iconSide", "isLoading", "href", "rel"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

import React, { isValidElement, useContext } from 'react';
import classNames from 'classnames';
import { useEuiMemoizedStyles } from '../../../services';
import { EuiButtonEmpty } from '../../button';
import { euiFormControlButtonStyles } from './form_control_button.styles';
import { useInnerText } from '../../inner_text';
import { EuiFormControlLayoutContext } from '../form_control_layout/form_control_layout_context';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiFormControlButton = function EuiFormControlButton(_ref) {
  var children = _ref.children,
    value = _ref.value,
    placeholder = _ref.placeholder,
    className = _ref.className,
    _contentProps = _ref.contentProps,
    _textProps = _ref.textProps,
    _compressed = _ref.compressed,
    _isDisabled = _ref.isDisabled,
    _isInvalid = _ref.isInvalid,
    _ref$fullWidth = _ref.fullWidth,
    fullWidth = _ref$fullWidth === void 0 ? true : _ref$fullWidth,
    _iconSide = _ref.iconSide,
    _isLoading = _ref.isLoading,
    href = _ref.href,
    rel = _ref.rel,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useInnerText = useInnerText(),
    _useInnerText2 = _slicedToArray(_useInnerText, 2),
    buttonTextRef = _useInnerText2[0],
    innerText = _useInnerText2[1];
  var _useContext = useContext(EuiFormControlLayoutContext),
    formLayoutIsDisabled = _useContext.isDisabled,
    formLayoutIsInvalid = _useContext.isInvalid,
    formLayoutIsLoading = _useContext.isLoading,
    formLayoutReadOnly = _useContext.readOnly,
    formLayoutCompressed = _useContext.compressed;
  var isDisabled = _isDisabled !== null && _isDisabled !== void 0 ? _isDisabled : formLayoutIsDisabled;
  var isInvalid = _isInvalid !== null && _isInvalid !== void 0 ? _isInvalid : formLayoutIsInvalid;
  var isLoading = formLayoutIsLoading === true ? false : _isLoading;
  var compressed = _compressed !== null && _compressed !== void 0 ? _compressed : formLayoutCompressed;
  var hasText = value || placeholder;
  var iconSide = isLoading ? 'right' : _iconSide;
  var styles = useEuiMemoizedStyles(euiFormControlButtonStyles);
  var classes = classNames('euiFormControlButton', className);
  var cssStyles = [styles.euiFormControlButton, isInvalid && styles.isInvalid, isLoading && styles.isLoading, formLayoutReadOnly && styles.readOnly, compressed && styles.compressed, fullWidth ? styles.fullWidth : styles.formWidth];
  var contentProps = _objectSpread(_objectSpread({}, _contentProps), {}, {
    css: [styles.euiFormControlButton__content, !hasText && iconSide === 'right' && styles.alignEnd, _contentProps === null || _contentProps === void 0 ? void 0 : _contentProps.css]
  });
  var customTextProps = _objectSpread(_objectSpread({}, _textProps), {}, {
    className: classNames('eui-textTruncate', _textProps && (_textProps === null || _textProps === void 0 ? void 0 : _textProps.className)),
    css: [styles.textContent, !value && styles.placeholder, _textProps && (_textProps === null || _textProps === void 0 ? void 0 : _textProps.css)]
  });
  var content = children ? /*#__PURE__*/isValidElement(children) ? children : ___EmotionJSX("span", null, children) : null;
  var linkProps = _objectSpread({
    href: href,
    rel: rel
  }, rest);
  var buttonProps = _objectSpread({
    value: value ? innerText !== null && innerText !== void 0 ? innerText : '' : undefined
  }, rest);
  var restProps = href ? linkProps : buttonProps;
  return ___EmotionJSX(EuiButtonEmpty, _extends({
    css: cssStyles,
    className: classes,
    contentProps: contentProps,
    textProps: false,
    color: "text",
    isDisabled: isDisabled,
    isLoading: isLoading,
    iconSide: isLoading ? 'right' : iconSide
  }, restProps), hasText && ___EmotionJSX("span", _extends({}, customTextProps, {
    ref: buttonTextRef
  }), value || placeholder), hasText && content && ' ', content);
};