import { _ as _defineProperty } from '../defineProperty-CEd-kZQW.js';
import { _ as _slicedToArray } from '../slicedToArray-BB-AwwkK.js';
import React__default, { useState, useEffect } from 'react';
import { useFilterTypes, useFilterType } from './filter-type-context.js';
import Button from '../button/index.js';
import SelectMenu, { separator } from '../select-menu/index.js';
import '../extends-C4cduGrv.js';
import '../objectWithoutProperties-TXwlfNxG.js';
import 'prop-types';
import '@reach/visually-hidden';
import '../ck-icon/index.js';
import 'classnames';
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';
import '../select-menu-BvA3x4Nf.js';
import '../_commonjsHelpers-_1FafXWI.js';
import '../label/index.js';
import '../portal/index.js';
import '../use-rerender-once/index.js';
import '../use-force-update/index.js';
import '../select-menu/autocomplete.js';

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 filterTypeToOption = function filterTypeToOption(filterType) {
  return filterType.type === "separator" ? separator : {
    value: filterType.type,
    label: filterType.label
  };
};

function EditFilter() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      filter = _ref.filter,
      onSave = _ref.onSave,
      onCancel = _ref.onCancel;

  var _useState = useState(filter),
      _useState2 = _slicedToArray(_useState, 2),
      filterState = _useState2[0],
      setFilterState = _useState2[1];

  var filterTypes = useFilterTypes();
  var filterType = useFilterType(filterState.type);

  var validate = filterType.validate || function () {
    return true;
  };

  var options = filterTypes.map(filterTypeToOption);
  useEffect(function () {
    setFilterState(filter);
  }, [filter]);

  var handleSave = function handleSave() {
    if (!validate(filterState)) return;
    onSave(filterState);
  };

  var handleChangeType = function handleChangeType(event) {
    var value = event.target.value;
    var type = filterTypes.find(function (filterType) {
      return filterType.type === value;
    });
    setFilterState(_objectSpread({
      type: value
    }, type.filter.defaults));
  };

  var Edit = filterType.Edit;
  return /*#__PURE__*/React__default.createElement("div", {
    className: "w-64"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "p-4"
  }, /*#__PURE__*/React__default.createElement("fieldset", null, /*#__PURE__*/React__default.createElement("legend", {
    className: "mb-1 text-sm text-gray-800 font-semibold border-none"
  }, "Add Filter"), /*#__PURE__*/React__default.createElement(SelectMenu, {
    "aria-label": "filter type",
    className: "w-full",
    onChange: handleChangeType,
    name: "type",
    value: filterState.type,
    options: options
  }), /*#__PURE__*/React__default.createElement(Edit, {
    filter: filterState,
    setFilter: setFilterState
  }))), /*#__PURE__*/React__default.createElement("div", {
    className: "p-4 border-t border-gray-300 flex"
  }, /*#__PURE__*/React__default.createElement(Button, {
    onClick: onCancel,
    className: "focus:outline-none text-sm text-gray-700 border border-transparent"
  }, "Cancel"), /*#__PURE__*/React__default.createElement("div", {
    className: "flex-grow"
  }), /*#__PURE__*/React__default.createElement(Button, {
    is: "primary",
    onClick: handleSave,
    disabled: !validate(filterState)
  }, "Add Filter")));
}

export { EditFilter as default };
