"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Dropdown = Dropdown;
exports.DropdownListbox = void 0;

var _react = require("react");

var _react2 = require("@headlessui/react");

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

var _reactHookForm = require("react-hook-form");

var _lodash = require("lodash");

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

var _excluded = ["className", "label", "control", "items", "name", "onChange", "value", "placeholder", "emptyable", "prefix", "suffix", "labelPrefix", "labelSuffix"],
    _excluded2 = ["onChange", "value", "label", "items", "prefix", "suffix", "labelPrefix", "labelSuffix"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : 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 _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 classNames() {
  for (var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++) {
    classes[_key] = arguments[_key];
  }

  return classes.filter(Boolean).join(" ");
}

function Dropdown(_ref) {
  var className = _ref.className,
      label = _ref.label,
      control = _ref.control,
      _ref$items = _ref.items,
      items = _ref$items === void 0 ? {} : _ref$items,
      name = _ref.name,
      _ref$onChange = _ref.onChange,
      onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
      _ref$value = _ref.value,
      value = _ref$value === void 0 ? null : _ref$value,
      placeholder = _ref.placeholder,
      emptyable = _ref.emptyable,
      _ref$prefix = _ref.prefix,
      prefix = _ref$prefix === void 0 ? "" : _ref$prefix,
      _ref$suffix = _ref.suffix,
      suffix = _ref$suffix === void 0 ? "" : _ref$suffix,
      _ref$labelPrefix = _ref.labelPrefix,
      labelPrefix = _ref$labelPrefix === void 0 ? "" : _ref$labelPrefix,
      _ref$labelSuffix = _ref.labelSuffix,
      labelSuffix = _ref$labelSuffix === void 0 ? "" : _ref$labelSuffix,
      rest = _objectWithoutProperties(_ref, _excluded);

  // eslint-disable-next-line no-underscore-dangle
  var _items = items;

  if ((0, _lodash.isArray)(items)) {
    _items = {};
    items.forEach(function (item) {
      var i = {
        label: item.label || item.description,
        name: item.name || item.id,
        id: item.id || item.name || item.value,
        key: item.id || item.name || item.value,
        icon: item.icon,
        description: item.description,
        value: item.value
      };
      _items[i.id] = i;
    });
  }

  items = _items; //  check if has placeholder

  if (placeholder) {
    var placeholderObj = {
      description: placeholder,
      name: "none",
      id: "none",
      key: "none",
      value: null
    };
    items = _objectSpread({
      null: placeholderObj
    }, items);
  } // check if emptyable


  if (emptyable && Object.values(items).length === 0) {
    var emptyObj = {
      description: "No available selections",
      name: "_empty",
      id: "_empty",
      key: "_empty",
      value: null
    };
    items = _objectSpread({
      null: emptyObj
    }, items);
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: className,
      children: control ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
        control: control,
        name: name,
        render: function render(_ref2) {
          var field = _ref2.field;
          var onChangeControlled = field.onChange,
              valueControlled = field.value;

          if (items[valueControlled] == null) {
            return null;
          }

          return /*#__PURE__*/(0, _jsxRuntime.jsx)(DropdownListbox, {
            onChange: onChangeControlled,
            value: valueControlled,
            label: label,
            items: items,
            rest: rest
          });
        }
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(DropdownListbox, {
        label: label,
        items: items,
        rest: rest,
        onChange: onChange,
        value: value,
        prefix: prefix,
        suffix: suffix,
        labelPrefix: labelPrefix,
        labelSuffix: labelSuffix
      })
    })
  });
}

var DropdownListbox = function DropdownListbox(_ref3) {
  var _onChange = _ref3.onChange,
      value = _ref3.value,
      label = _ref3.label,
      items = _ref3.items,
      _ref3$prefix = _ref3.prefix,
      prefix = _ref3$prefix === void 0 ? "" : _ref3$prefix,
      _ref3$suffix = _ref3.suffix,
      suffix = _ref3$suffix === void 0 ? "" : _ref3$suffix,
      _ref3$labelPrefix = _ref3.labelPrefix,
      labelPrefix = _ref3$labelPrefix === void 0 ? "" : _ref3$labelPrefix,
      _ref3$labelSuffix = _ref3.labelSuffix,
      labelSuffix = _ref3$labelSuffix === void 0 ? "" : _ref3$labelSuffix,
      rest = _objectWithoutProperties(_ref3, _excluded2);

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Listbox, _objectSpread(_objectSpread({
    value: value,
    onChange: function onChange(e) {
      _onChange(e);
    }
  }, rest), {}, {
    children: function children(_ref4) {
      var _items$value;

      var open = _ref4.open;
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [label && /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Listbox.Label, {
          as: "span",
          className: "text-sm font-medium text-gray-700",
          children: label
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "".concat(label && "mt-1", " relative"),
          children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_react2.Listbox.Button, {
            className: "relative cursor-pointer w-full bg-white border border-gray-300 rounded-md shadow-sm pl-3 pr-10 py-2 text-left  focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm \n              ".concat(((_items$value = items[value]) === null || _items$value === void 0 ? void 0 : _items$value.id) === "_empty" && "text-gray-400", "\n            "),
            children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
              className: "flex items-center",
              children: [prefix, items[value] && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
                children: items[value].icon
              }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
                className: "ml-2 block truncate",
                children: [labelPrefix, items[value] && items[value].description, labelSuffix]
              }), suffix]
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
              className: "absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none",
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_solid.SelectorIcon, {
                className: "h-5 w-5 text-gray-400",
                "aria-hidden": "true"
              })
            })]
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Transition, {
            show: open,
            as: _react.Fragment,
            leave: "transition ease-in duration-100",
            leaveFrom: "opacity-100",
            leaveTo: "opacity-0",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Listbox.Options, {
              className: "absolute z-10 mt-1 w-full bg-white shadow-lg max-h-60 rounded-md py-1 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm",
              children: Object.values(items).sort(function (a, b) {
                if (Object.prototype.hasOwnProperty.call(a, "order") && Object.prototype.hasOwnProperty.call(b, "order")) {
                  return a.order - b.order;
                }

                if (Object.prototype.hasOwnProperty.call(a, "order")) {
                  return -1;
                }

                if (Object.prototype.hasOwnProperty.call(b, "order")) {
                  return 1;
                }

                return 0;
              }).map(function (item) {
                return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Listbox.Option, {
                  className: function className(_ref5) {
                    var active = _ref5.active;
                    return classNames(active ? "text-white bg-indigo-600" : "text-gray-900", "cursor-pointer select-none relative py-2 pl-3 pr-9");
                  },
                  value: item.value,
                  children: function children(_ref6) {
                    var selected = _ref6.selected,
                        active = _ref6.active;
                    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
                      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
                        className: "flex items-center",
                        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
                          children: item.icon
                        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                          className: classNames(selected ? "font-semibold" : "font-normal", "ml-2 block truncate"),
                          children: item.description
                        })]
                      }), selected ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                        className: classNames(active ? "text-white" : "text-indigo-600", "absolute inset-y-0 right-0 flex items-center pr-4"),
                        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_solid.CheckIcon, {
                          className: "h-5 w-5",
                          "aria-hidden": "true"
                        })
                      }) : null]
                    });
                  }
                }, item.id);
              })
            })
          })]
        })]
      });
    }
  }));
};

exports.DropdownListbox = DropdownListbox;