import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.object.to-string.js';
import React__default from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { settings } from '../../constants/Settings.js';
import Button from '../Button/Button.js';
import { MapControlPropType } from '../../constants/CardPropTypes.js';
import ScrollingControls from './ScrollingControls.js';
import { Layers, CaretSortDown } from '@carbon/icons-react';

var iotPrefix = settings.iotPrefix;
var BASE_CLASS_NAME = "".concat(iotPrefix, "--map-controls");
var propTypes = {
  /** list of map control buttons */
  controls: PropTypes.arrayOf(MapControlPropType),
  /** list of layered map control buttons that are expanded horizontally */
  layeredControls: PropTypes.arrayOf(MapControlPropType),
  i18n: PropTypes.shape({
    layerTriggerIconDescription: PropTypes.string,
    scrollUp: PropTypes.string,
    scrollDown: PropTypes.string
  }),
  /** true if the map controls are in an expanded card */
  isExpandedMode: PropTypes.bool,
  testId: PropTypes.string,
  /** position of the control buttons tooltip */
  tooltipPosition: PropTypes.oneOf(['left', 'top', 'bottom', 'right'])
};
var defaultProps = {
  controls: [],
  layeredControls: [],
  i18n: {
    layerTriggerIconDescription: 'Layered controls',
    scrollUp: 'Scroll up',
    scrollDown: 'Scroll down'
  },
  isExpandedMode: false,
  testId: 'map-controls',
  tooltipPosition: 'left'
};
var MapControls = function MapControls(_ref) {
  var layeredControls = _ref.layeredControls,
    controls = _ref.controls,
    tooltipPosition = _ref.tooltipPosition,
    isExpandedMode = _ref.isExpandedMode,
    i18n = _ref.i18n,
    testId = _ref.testId;
  var layerTriggerIconDescription = i18n.layerTriggerIconDescription,
    scrollUp = i18n.scrollUp,
    scrollDown = i18n.scrollDown;
  var _React$useState = React__default.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    layersOpen = _React$useState2[0],
    setLayersOpen = _React$useState2[1];
  var handleLayerClick = function handleLayerClick() {
    setLayersOpen(function (openState) {
      return !openState;
    });
  };
  var buttonSize = isExpandedMode ? 'md' : 'sm';
  var renderControl = function renderControl(control, index) {
    var _control$kind;
    if (control.group) {
      var groupClass = "".concat(BASE_CLASS_NAME, "-btn-group");
      var groupKey = control.group.reduce(function (key, childControl) {
        return "".concat(key).concat(childControl.iconDescription);
      }, index);
      return control.hasScroll ? isExpandedMode ? /*#__PURE__*/React__default.createElement(ScrollingControls, {
        testId: "".concat(testId, "-scroll-controls"),
        key: groupKey,
        controls: control.group,
        classname: groupClass,
        scrollUpIconDescriptionText: scrollUp,
        scrollDownIconDescriptionText: scrollDown,
        tooltipPosition: tooltipPosition,
        visibleItemsCount: control.visibleItemsCount
      }) : null : /*#__PURE__*/React__default.createElement("div", {
        key: groupKey,
        className: groupClass
      }, control.group.map(function (groupedControl, myIndex) {
        var _groupedControl$kind;
        return /*#__PURE__*/React__default.createElement(Button, {
          key: "".concat(groupedControl.iconDescription, "-").concat(myIndex),
          className: "".concat(BASE_CLASS_NAME, "-btn"),
          kind: (_groupedControl$kind = groupedControl.kind) !== null && _groupedControl$kind !== void 0 ? _groupedControl$kind : 'ghost',
          size: buttonSize,
          selected: groupedControl.selected,
          hasIconOnly: true,
          tooltipPosition: tooltipPosition,
          renderIcon: groupedControl.icon,
          iconDescription: groupedControl.iconDescription,
          onClick: groupedControl.onClick
        });
      }));
    }
    return /*#__PURE__*/React__default.createElement(Button, {
      key: "".concat(control.iconDescription, "-").concat(index),
      className: "".concat(BASE_CLASS_NAME, "-btn"),
      kind: (_control$kind = control.kind) !== null && _control$kind !== void 0 ? _control$kind : 'ghost',
      size: buttonSize,
      selected: control.selected,
      hasIconOnly: true,
      tooltipPosition: tooltipPosition,
      renderIcon: control.icon,
      iconDescription: control.iconDescription,
      onClick: control.onClick
    });
  };
  var renderLayers = function renderLayers(control, index) {
    var _control$kind2;
    return /*#__PURE__*/React__default.createElement(Button, {
      key: "".concat(control.iconDescription, "-").concat(index),
      className: "".concat(BASE_CLASS_NAME, "-layers-btn"),
      kind: (_control$kind2 = control.kind) !== null && _control$kind2 !== void 0 ? _control$kind2 : 'ghost',
      size: buttonSize,
      selected: control.selected,
      hasIconOnly: true,
      tooltipPosition: tooltipPosition,
      renderIcon: control.icon,
      iconDescription: control.iconDescription,
      onClick: control.onClick,
      tabIndex: layersOpen ? 0 : -1
    });
  };
  return /*#__PURE__*/React__default.createElement("div", {
    "data-testid": testId,
    className: classnames("".concat(BASE_CLASS_NAME, "__container"), _defineProperty({}, "".concat(BASE_CLASS_NAME, "__container--has-layers"), layeredControls.length))
  }, layeredControls.length !== 0 && /*#__PURE__*/React__default.createElement("div", {
    className: classnames("".concat(BASE_CLASS_NAME, "-layers"), _defineProperty({}, "".concat(BASE_CLASS_NAME, "-layers--open"), layersOpen))
  }, /*#__PURE__*/React__default.createElement(Button, {
    className: "".concat(BASE_CLASS_NAME, "-layers-trigger"),
    kind: "ghost",
    size: buttonSize,
    hasIconOnly: true,
    tooltipPosition: tooltipPosition,
    renderIcon: Layers,
    iconDescription: layerTriggerIconDescription,
    onClick: handleLayerClick
  }, /*#__PURE__*/React__default.createElement(CaretSortDown, {
    className: "".concat(BASE_CLASS_NAME, "-layers-trigger__caret")
  })), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(BASE_CLASS_NAME, "-layers-wrapper")
  }, layeredControls.map(renderLayers))), controls.map(renderControl));
};
MapControls.propTypes = propTypes;
MapControls.defaultProps = defaultProps;
MapControls.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "MapControls",
  "props": {
    "controls": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "list of map control buttons",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "custom",
          "raw": "MapControlPropType"
        }
      },
      "required": false
    },
    "layeredControls": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "list of layered map control buttons that are expanded horizontally",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "custom",
          "raw": "MapControlPropType"
        }
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  layerTriggerIconDescription: 'Layered controls',\n  scrollUp: 'Scroll up',\n  scrollDown: 'Scroll down',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "layerTriggerIconDescription": {
            "name": "string",
            "required": false
          },
          "scrollUp": {
            "name": "string",
            "required": false
          },
          "scrollDown": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "isExpandedMode": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "true if the map controls are in an expanded card",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'map-controls'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "tooltipPosition": {
      "defaultValue": {
        "value": "'left'",
        "computed": false
      },
      "description": "position of the control buttons tooltip",
      "type": {
        "name": "enum",
        "value": [{
          "value": "'left'",
          "computed": false
        }, {
          "value": "'top'",
          "computed": false
        }, {
          "value": "'bottom'",
          "computed": false
        }, {
          "value": "'right'",
          "computed": false
        }]
      },
      "required": false
    }
  }
};
var MapControls$1 = MapControls;

export { MapControls$1 as default };
