"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.ColorPicker = void 0;
var _colord = require("colord");
var _names = _interopRequireDefault(require("colord/plugins/names"));
var _element = require("@wordpress/element");
var _compose = require("@wordpress/compose");
var _i18n = require("@wordpress/i18n");
var _context = require("../context");
var _styles = require("./styles");
var _colorCopyButton = require("./color-copy-button");
var _colorInput = require("./color-input");
var _picker = require("./picker");
var _hooks = require("../utils/hooks");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

(0, _colord.extend)([_names.default]);
const options = [{
  label: 'RGB',
  value: 'rgb'
}, {
  label: 'HSL',
  value: 'hsl'
}, {
  label: 'Hex',
  value: 'hex'
}];
const UnconnectedColorPicker = (props, forwardedRef) => {
  const {
    enableAlpha = false,
    color: colorProp,
    onChange,
    defaultValue = '#fff',
    copyFormat,
    ...divProps
  } = (0, _context.useContextSystem)(props, 'ColorPicker');

  // Use a safe default value for the color and remove the possibility of `undefined`.
  const [color, setColor] = (0, _hooks.useControlledValue)({
    onChange,
    value: colorProp,
    defaultValue
  });
  const safeColordColor = (0, _element.useMemo)(() => {
    return (0, _colord.colord)(color || '');
  }, [color]);
  const debouncedSetColor = (0, _compose.useDebounce)(setColor);
  const handleChange = (0, _element.useCallback)(nextValue => {
    debouncedSetColor(nextValue.toHex());
  }, [debouncedSetColor]);
  const [colorType, setColorType] = (0, _element.useState)(copyFormat || 'hex');
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styles.ColorfulWrapper, {
    ref: forwardedRef,
    ...divProps,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_picker.Picker, {
      onChange: handleChange,
      color: safeColordColor,
      enableAlpha: enableAlpha
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styles.AuxiliaryColorArtefactWrapper, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_styles.AuxiliaryColorArtefactHStackHeader, {
        justify: "space-between",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.SelectControl, {
          __nextHasNoMarginBottom: true,
          size: "compact",
          options: options,
          value: colorType,
          onChange: nextColorType => setColorType(nextColorType),
          label: (0, _i18n.__)('Color format'),
          hideLabelFromVision: true,
          variant: "minimal"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_colorCopyButton.ColorCopyButton, {
          color: safeColordColor,
          colorType: copyFormat || colorType
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.ColorInputWrapper, {
        direction: "column",
        gap: 2,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_colorInput.ColorInput, {
          colorType: colorType,
          color: safeColordColor,
          onChange: handleChange,
          enableAlpha: enableAlpha
        })
      })]
    })]
  });
};
const ColorPicker = exports.ColorPicker = (0, _context.contextConnect)(UnconnectedColorPicker, 'ColorPicker');
var _default = exports.default = ColorPicker;
//# sourceMappingURL=component.js.map