import { _ as _extends } from '../extends-C4cduGrv.js';
import { _ as _objectWithoutProperties } from '../objectWithoutProperties-TXwlfNxG.js';
import { _ as _defineProperty } from '../defineProperty-CEd-kZQW.js';
import { _ as _slicedToArray } from '../slicedToArray-BB-AwwkK.js';
import React__default, { useState, useCallback, useContext, useEffect } from 'react';
import cx from 'classnames';
import { u as useId } from '../auto-id.esm-Cly76abB.js';
import Button from '../button/index.js';
import '../utils.esm-CKyxlHOy.js';
import '../warning-s9zGJbnw.js';
import 'prop-types';
import '@reach/visually-hidden';
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 '../floating-ui.react-cWPyyUrN.js';
import '../typeof-CgC7Bq6K.js';
import '../toConsumableArray-dKQYsfpV.js';
import '../index-qSfOyGhk.js';
import 'react-dom';
import '../button/button-group.js';

var _excluded = ["id"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var ActionContext = /*#__PURE__*/React__default.createContext({
  actions: [],
  register: function register() {},
  remove: function remove() {},
  update: function update() {}
});
var ActionProvider = function ActionProvider(_ref) {
  var children = _ref.children;

  var _useState = useState([]),
      _useState2 = _slicedToArray(_useState, 2),
      actions = _useState2[0],
      setActions = _useState2[1];

  var remove = useCallback(function (id) {
    setActions(function (actions) {
      return actions.filter(function (action) {
        return action.id !== id;
      });
    });
  }, []);
  var register = useCallback(function (id, props, ref) {
    setActions(function (actions) {
      var index = actions.findIndex(function (action) {
        return action.id === id;
      });
      if (index == -1) index = actions.length;
      return Object.assign([], actions, _defineProperty({}, index, _objectSpread(_objectSpread({
        id: id
      }, actions[index]), _objectSpread(_objectSpread({}, props), {}, {
        ref: ref
      }))));
    });
  }, []);
  return /*#__PURE__*/React__default.createElement(ActionContext.Provider, {
    value: {
      actions: actions,
      register: register,
      remove: remove
    }
  }, children);
};

var useAction = function useAction(id, props, ref) {
  var _useContext = useContext(ActionContext),
      register = _useContext.register,
      remove = _useContext.remove;

  useEffect(function () {
    if (!id) return;
    register(id, props, ref);
    return function () {
      return remove(id);
    };
  }, [id, props, ref, register, remove]);
};

var Actions = function Actions() {
  var _useContext2 = useContext(ActionContext),
      actions = _useContext2.actions;

  if (actions.length === 0) return null;
  return /*#__PURE__*/React__default.createElement("div", {
    className: "flex justify-end rounded-b-lg border-t border-gray-200 py-6 px-6 sticky bottom-0 bg-white",
    "data-testid": "actions"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "-mr-2"
  }, actions.map(function (_ref2) {
    var id = _ref2.id,
        props = _objectWithoutProperties(_ref2, _excluded);

    return /*#__PURE__*/React__default.createElement(Button, _extends({
      key: id
    }, props, {
      className: cx("mx-2 flex-auto justify-center font-semibold", props.className)
    }));
  })));
};
var Action = /*#__PURE__*/React__default.forwardRef(function (props, ref) {
  var id = useId();
  useAction(id, props, ref);
  return null;
});

export { ActionContext, ActionProvider, Actions, Action as default };
