'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var neetoCist = require('@bigbinary/neeto-cist');
var general = require('@bigbinary/neeto-commons-frontend/utils/general');
var Braces = require('@bigbinary/neeto-icons/Braces');
var Dropdown = require('@bigbinary/neetoui/Dropdown');
var Typography = require('@bigbinary/neetoui/Typography');
var ramda = require('ramda');
var injectCss = require('./inject-css-B6qYtOJe.js');
var jsxRuntime = require('react/jsx-runtime');

var css = ".neeto-molecules-dynamic-variables{display:flex;flex-direction:column;gap:.375rem;max-width:22.5rem;min-width:11.5rem;padding:.5rem}.neeto-molecules-dynamic-variables__list{align-items:center;display:flex;flex-wrap:wrap;gap:.5rem}.neeto-molecules-dynamic-variables__list button{border:1px solid rgb(var(--neeto-ui-gray-300));border-radius:var(--neeto-ui-rounded-md);color:rgb(var(--neeto-ui-black));padding:.375rem;text-align:start;transition:var(--neeto-ui-transition);-webkit-user-select:none;-moz-user-select:none;user-select:none}.neeto-molecules-dynamic-variables__list button:focus,.neeto-molecules-dynamic-variables__list button:focus-visible,.neeto-molecules-dynamic-variables__list button:hover{border-color:rgb(var(--neeto-ui-primary-800));box-shadow:none;color:rgb(var(--neeto-ui-primary-800));outline:none}";
injectCss.n(css,{});

var parseVariables = function parseVariables() {
  var variableArr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var uncategorized = [];
  var groupedVariables = [];
  var categorySet = new Set();
  variableArr.forEach(function (variable) {
    var category = variable.category,
      label = variable.label,
      variables = variable.variables;
    if (category && variables) {
      var parsedVariables = variables.map(ramda.mergeLeft({
        category: category,
        categoryLabel: label
      }));
      if (!categorySet.has(category)) {
        categorySet.add(category);
        groupedVariables.push({
          label: label,
          variables: parsedVariables
        });
      } else {
        var existingGroup = neetoCist.findBy({
          label: label
        }, groupedVariables);
        existingGroup.variables = ramda.uniqBy(ramda.prop("key"), existingGroup.variables.concat(parsedVariables));
      }
    } else {
      uncategorized.push(variable);
    }
  });
  if (!ramda.isEmpty(uncategorized)) {
    groupedVariables.push({
      label: !ramda.isEmpty(groupedVariables) ? "Others" : null,
      variables: uncategorized
    });
  }
  return groupedVariables;
};

var _excluded = ["onVariableClick", "variables", "buttonProps"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var Menu = Dropdown.Menu;
var DynamicVariables = function DynamicVariables(_ref) {
  var _ref$onVariableClick = _ref.onVariableClick,
    onVariableClick = _ref$onVariableClick === void 0 ? neetoCist.noop : _ref$onVariableClick,
    _ref$variables = _ref.variables,
    variables = _ref$variables === void 0 ? [] : _ref$variables,
    buttonProps = _ref.buttonProps,
    dropdownProps = _objectWithoutProperties(_ref, _excluded);
  if (ramda.isEmpty(variables)) {
    return null;
  }
  var parsedVariables = parseVariables(variables);
  return /*#__PURE__*/jsxRuntime.jsx(Dropdown, _objectSpread(_objectSpread({
    appendTo: function appendTo() {
      return document.body;
    },
    buttonStyle: "secondary",
    icon: Braces,
    strategy: "fixed",
    zIndex: 99999,
    buttonProps: _objectSpread({
      "data-testid": "variables-dropdown",
      "data-cy": "dynamic-variables-button"
    }, buttonProps)
  }, dropdownProps), {}, {
    children: /*#__PURE__*/jsxRuntime.jsx(Menu, {
      children: /*#__PURE__*/jsxRuntime.jsx("div", {
        className: "neeto-molecules-dynamic-variables",
        "data-cy": "dynamic-variables-list",
        "data-testid": "variables-list",
        children: parsedVariables.map(function (_ref2) {
          var label = _ref2.label,
            variables = _ref2.variables;
          return /*#__PURE__*/jsxRuntime.jsxs(React.Fragment, {
            children: [label && /*#__PURE__*/jsxRuntime.jsxs("div", {
              className: "flex flex-row items-center gap-x-2",
              children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
                style: "h6",
                children: label
              }), /*#__PURE__*/jsxRuntime.jsx("hr", {
                className: "neeto-ui-border-gray-200 flex flex-grow"
              })]
            }), /*#__PURE__*/jsxRuntime.jsx("div", {
              className: "neeto-molecules-dynamic-variables__list",
              "data-cy": general.joinHyphenCase("dynamic-variables-list", label),
              children: variables.map(function (item) {
                return /*#__PURE__*/jsxRuntime.jsx("button", {
                  "data-cy": general.joinHyphenCase("dynamic-variables-list-item", item.label),
                  onClick: function onClick() {
                    return onVariableClick(item);
                  },
                  children: item.label
                }, item.label);
              })
            })]
          }, label);
        })
      })
    })
  }));
};

module.exports = DynamicVariables;
//# sourceMappingURL=DynamicVariables.js.map
