"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextInputClearable = TextInputClearable;

var _solid = require("@heroicons/react/solid");

var _react = require("react");

var _io = require("react-icons/io5");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["className", "defaultValue", "onChange", "onSubmit", "inputClassName", "inputOuterClassName", "buttonIcon", "hideButton", "onClear", "placeholder", "hideClearButton"];

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; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function TextInputClearable(_ref) {
  var className = _ref.className,
      defaultValue = _ref.defaultValue,
      _ref$onChange = _ref.onChange,
      onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
      _ref$onSubmit = _ref.onSubmit,
      onSubmit = _ref$onSubmit === void 0 ? function () {} : _ref$onSubmit,
      inputClassName = _ref.inputClassName,
      inputOuterClassName = _ref.inputOuterClassName,
      buttonIcon = _ref.buttonIcon,
      hideButton = _ref.hideButton,
      onClear = _ref.onClear,
      placeholder = _ref.placeholder,
      _ref$hideClearButton = _ref.hideClearButton,
      hideClearButton = _ref$hideClearButton === void 0 ? false : _ref$hideClearButton,
      rest = _objectWithoutProperties(_ref, _excluded);

  var _useState = (0, _react.useState)(defaultValue || ""),
      _useState2 = _slicedToArray(_useState, 2),
      textValue = _useState2[0],
      setTextValue = _useState2[1];

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "flex ".concat(className),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "relative flex-grow ".concat(inputOuterClassName),
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", _objectSpread({
        defaultValue: defaultValue,
        onChange: onChange,
        type: "text",
        className: "h-full w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block py-1 sm:text-sm border-gray-300 ".concat(hideButton ? "rounded-md" : "rounded-l-md", " ").concat(inputClassName),
        placeholder: placeholder
      }, rest)), !hideClearButton && textValue && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "absolute inset-y-0 right-0 pr-2 flex items-center",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
          className: "p-1",
          type: "button",
          onClick: function onClick() {
            setTextValue("");

            if (onClear) {
              onClear();
            }
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_io.IoCloseCircle, {
            className: "h-5 w-5 text-gray-400",
            "aria-hidden": "true"
          })
        })
      })]
    }), !hideButton && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
      onClick: function onClick() {
        return onSubmit(textValue);
      },
      type: "submit",
      className: "py-2 px-3 border border-l-0 border-gray-300 rounded-r-md bg-gray-50 hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500",
      children: buttonIcon || /*#__PURE__*/(0, _jsxRuntime.jsx)(_solid.SearchIcon, {
        className: "h-4 w-4 text-gray-400",
        "aria-hidden": "true"
      })
    })]
  });
}