const _excluded = ["isLoading", "iconPosition", "className", "buttonType", "icon", "children"],
  _excluded2 = ["isLoading", "icon", "iconPosition", "className", "disabled", "buttonType", "children"],
  _excluded3 = ["className", "buttonType", "children"];
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 *
 * Any modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */

/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import PropTypes from 'prop-types';
import classNames from 'classnames';
import { KuiButtonIcon } from './button_icon/button_icon';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const accessibleIconButton = (props, propName, componentName) => {
  if (props.children) {
    return;
  }
  if (props['aria-label']) {
    return;
  }
  if (props['aria-labelledby']) {
    return;
  }
  throw new Error("".concat(componentName, " requires aria-label or aria-labelledby to be specified if it does not have children. ") + "This is because we're assuming you're creating an icon-only button, which is screen-reader-inaccessible.");
};
const BUTTON_TYPES = ['basic', 'hollow', 'danger', 'warning', 'primary', 'secondary'];
const ICON_POSITIONS = ['left', 'right'];
const DEFAULT_ICON_POSITION = 'left';
const buttonTypeToClassNameMap = {
  basic: 'kuiButton--basic',
  hollow: 'kuiButton--hollow',
  danger: 'kuiButton--danger',
  warning: 'kuiButton--warning',
  primary: 'kuiButton--primary',
  secondary: 'kuiButton--secondary'
};
const getClassName = _ref => {
  let {
    className,
    buttonType,
    hasIcon = false
  } = _ref;
  return classNames('kuiButton', className, buttonTypeToClassNameMap[buttonType], {
    'kuiButton--iconText': hasIcon
  });
};
const ContentWithIcon = _ref2 => {
  let {
    children,
    icon,
    iconPosition,
    isLoading
  } = _ref2;
  const iconOrLoading = isLoading ? /*#__PURE__*/_jsx(KuiButtonIcon, {
    type: "loading"
  }) : icon;

  // We need to wrap the children so that the icon's :first-child etc. pseudo-selectors get applied
  // correctly.
  const wrappedChildren = children ? /*#__PURE__*/_jsx("span", {
    children: children
  }) : undefined;
  switch (iconPosition) {
    case 'left':
      return /*#__PURE__*/_jsxs("span", {
        className: "kuiButton__inner",
        children: [iconOrLoading, wrappedChildren]
      });
    case 'right':
      return /*#__PURE__*/_jsxs("span", {
        className: "kuiButton__inner",
        children: [wrappedChildren, iconOrLoading]
      });
  }
};
const KuiButton = _ref3 => {
  let {
      isLoading,
      iconPosition = DEFAULT_ICON_POSITION,
      className,
      buttonType,
      icon,
      children
    } = _ref3,
    rest = _objectWithoutProperties(_ref3, _excluded);
  return /*#__PURE__*/_jsx("button", _objectSpread(_objectSpread({
    className: getClassName({
      className,
      buttonType,
      hasIcon: icon || isLoading
    })
  }, rest), {}, {
    children: /*#__PURE__*/_jsx(ContentWithIcon, {
      icon: icon,
      iconPosition: iconPosition,
      isLoading: isLoading,
      children: children
    })
  }));
};
KuiButton.propTypes = {
  icon: PropTypes.node,
  iconPosition: PropTypes.oneOf(ICON_POSITIONS),
  children: PropTypes.node,
  isLoading: PropTypes.bool,
  buttonType: PropTypes.oneOf(BUTTON_TYPES),
  className: PropTypes.string,
  'aria-label': accessibleIconButton
};
const KuiLinkButton = _ref4 => {
  let {
      isLoading,
      icon,
      iconPosition = DEFAULT_ICON_POSITION,
      className,
      disabled,
      buttonType,
      children
    } = _ref4,
    rest = _objectWithoutProperties(_ref4, _excluded2);
  const onClick = e => {
    if (disabled) {
      e.preventDefault();
    }
  };
  const classes = classNames(getClassName({
    className,
    buttonType,
    hasIcon: icon || isLoading
  }), {
    'kuiButton-isDisabled': disabled
  });
  return /*#__PURE__*/_jsx("a", _objectSpread(_objectSpread({
    className: classes,
    onClick: onClick
  }, rest), {}, {
    children: /*#__PURE__*/_jsx(ContentWithIcon, {
      icon: icon,
      iconPosition: iconPosition,
      isLoading: isLoading,
      children: children
    })
  }));
};
KuiLinkButton.propTypes = {
  icon: PropTypes.node,
  iconPosition: PropTypes.oneOf(ICON_POSITIONS),
  isLoading: PropTypes.bool,
  buttonType: PropTypes.oneOf(BUTTON_TYPES),
  className: PropTypes.string,
  children: PropTypes.node,
  'aria-label': accessibleIconButton
};
const KuiSubmitButton = _ref5 => {
  let {
      className,
      buttonType,
      children
    } = _ref5,
    rest = _objectWithoutProperties(_ref5, _excluded3);
  // NOTE: The `input` element is a void element and can't contain children.
  return /*#__PURE__*/_jsx("input", _objectSpread({
    type: "submit",
    value: children,
    className: getClassName({
      className,
      buttonType
    })
  }, rest));
};
KuiSubmitButton.propTypes = {
  children: PropTypes.string,
  buttonType: PropTypes.oneOf(BUTTON_TYPES),
  className: PropTypes.string
};
export { BUTTON_TYPES, KuiButton, KuiLinkButton, KuiSubmitButton };
