import { _ as _extends } from '../extends-C4cduGrv.js';
import { _ as _slicedToArray } from '../slicedToArray-BB-AwwkK.js';
import React__default, { useState, useCallback, useEffect } from 'react';
import { c as common, C as ColorPicker } from '../color-picker-CvIUD2KT.js';
import PropTypes from 'prop-types';
import ColorPickerInputGroup from './color-picker-input-group.js';
import cx from 'classnames';
import getColorInHex from './get-color-in-hex.js';
import { SavedColorShape } from './saved-colors-addon.js';
import { u as usePopper } from '../usePopper-DMhEmCof.js';
import Portal from '../portal/index.js';
import Label from '../label/index.js';
import '../defineProperty-CEd-kZQW.js';
import '../objectWithoutProperties-TXwlfNxG.js';
import '../_baseIteratee-D8btQgSF.js';
import '../typeof-CgC7Bq6K.js';
import '../_commonjsHelpers-_1FafXWI.js';
import '../input/index.js';
import '../use-previous/index.js';
import '../ck-icon/index.js';
import '../ck-icon/assets/icon-map/index.js';
import '../icon/index.js';
import '@fortawesome/react-fontawesome';
import '../icon/icons.js';
import '../icon/library.js';
import '@fortawesome/fontawesome-svg-core';
import '@fortawesome/pro-solid-svg-icons';
import '@fortawesome/pro-light-svg-icons';
import '@fortawesome/free-brands-svg-icons';
import '../tooltip/index.js';
import '../auto-id.esm-Cly76abB.js';
import '../utils.esm-CKyxlHOy.js';
import '../warning-s9zGJbnw.js';
import '../portal.esm-CuKGPwhL.js';
import 'react-dom';
import '@reach/visually-hidden';
import '../use-rerender-once/index.js';
import '../use-force-update/index.js';

var ColorInput = function ColorInput(_ref) {
  var label = _ref.label,
      onChange = _ref.onChange,
      transparency = _ref.transparency,
      value = _ref.value,
      savedColors = _ref.savedColors,
      children = _ref.children;

  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      colorInput = _useState2[0],
      setColorInput = _useState2[1];

  var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      colorPicker = _useState4[0],
      setColorPicker = _useState4[1];

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      active = _useState6[0],
      setActive = _useState6[1];

  var _usePopper = usePopper(colorInput, colorPicker, {
    placement: "bottom-start",
    modifiers: [{
      name: "flip",
      enabled: true
    }]
  }),
      styles = _usePopper.styles,
      attributes = _usePopper.attributes;

  var buttonClasses = cx("flex items-center w-full bg-white border border-gray-200 rounded text-gray-800 focus:outline-none", {
    uppercase: getColorInHex(value) !== "transparent"
  });

  var handleClick = function handleClick() {
    if (active) {
      return;
    }

    setActive(true);
  };

  var handleChange = function handleChange(color) {
    onChange && onChange(color);
  };

  var handleClose = function handleClose() {
    setActive(false);
  };

  var handleDocumentClick = useCallback(function (event) {
    if (!colorInput) return;
    if (!colorPicker) return; // Close if the the click is not inside the color input or color picker

    if ((!colorInput.contains(event.target) || colorInput === event.target) && (!colorPicker.contains(event.target) || colorPicker === event.target)) {
      handleClose();
    }
  }, [colorPicker, colorInput]);
  useEffect(function () {
    document.addEventListener("mousedown", handleDocumentClick);
    return function () {
      document.removeEventListener("mousedown", handleDocumentClick);
    };
  }, [handleDocumentClick]);
  var hex = getColorInHex(value);
  var swatchClass = hex === "#FFFFFF" ? " border border-gray-200" : "";
  return /*#__PURE__*/React__default.createElement("div", {
    className: "relative",
    ref: setColorInput
  }, label && /*#__PURE__*/React__default.createElement(Label, {
    as: "div",
    className: "mb-1"
  }, label), /*#__PURE__*/React__default.createElement("div", {
    className: buttonClasses,
    onClick: handleClick
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "rounded overflow-hidden ml-2 flex-none h-6 w-6" + swatchClass
  }, /*#__PURE__*/React__default.createElement(common.Swatch, {
    color: hex
  })), /*#__PURE__*/React__default.createElement(ColorPickerInputGroup, {
    color: hex,
    onChange: handleChange
  })), /*#__PURE__*/React__default.createElement(Portal, null, active && /*#__PURE__*/React__default.createElement("div", _extends({
    ref: setColorPicker,
    style: styles.popper
  }, attributes.popper), /*#__PURE__*/React__default.createElement(ColorPicker, {
    onChange: handleChange,
    onClose: handleClose,
    active: active,
    color: hex,
    hexInput: false,
    className: "mt-1",
    position: "relative",
    transparency: transparency,
    standalone: false,
    savedColors: savedColors
  }, children))));
};

ColorInput.propTypes = {
  label: PropTypes.string,
  onChange: PropTypes.func,
  value: PropTypes.string,
  transparency: PropTypes.bool,
  savedColors: PropTypes.shape(SavedColorShape),
  children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node])
};

export { ColorInput as default };
