'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var neetoAtoms = require('@bigbinary/neeto-atoms');
var neetoCist = require('@bigbinary/neeto-cist');
var general = require('@bigbinary/neeto-commons-frontend/v2/utils/general');
var ramda = require('ramda');
var jsxRuntime = require('react/jsx-runtime');
var createLucideIcon = require('../createLucideIcon-DBDhE7kE.js');

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode = [
  [
    "path",
    { d: "M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1", key: "ezmyqa" }
  ],
  [
    "path",
    {
      d: "M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1",
      key: "e1hn23"
    }
  ]
];
const Braces = createLucideIcon.createLucideIcon("braces", __iconNode);

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 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(neetoAtoms.DropdownMenu, _objectSpread(_objectSpread({
    customTarget: /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Button, _objectSpread({
      "data-testid": "dynamic-variables-button",
      icon: Braces,
      size: "icon",
      variant: "outline"
    }, buttonProps))
  }, dropdownProps), {}, {
    dropdownProps: _objectSpread({
      className: "min-w-56"
    }, dropdownProps === null || dropdownProps === void 0 ? void 0 : dropdownProps.dropdownProps),
    children: /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "flex max-w-[22.5rem] min-w-[11.5rem] flex-col gap-1.5 p-2",
      "data-testid": "dynamic-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(neetoAtoms.Typography, {
              variant: "h6",
              children: label
            }), /*#__PURE__*/jsxRuntime.jsx("hr", {
              className: "border-border flex flex-grow"
            })]
          }), /*#__PURE__*/jsxRuntime.jsx("div", {
            className: "flex flex-wrap items-center gap-2",
            "data-testid": general.joinHyphenCase("dynamic-variables-list", label),
            children: variables.map(function (item) {
              return /*#__PURE__*/jsxRuntime.jsx("button", {
                className: "border-border text-foreground hover:border-primary hover:text-primary focus-visible:border-primary focus-visible:text-primary rounded-md border p-1.5 text-start transition-colors select-none focus:outline-none",
                "data-testid": 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
