'use strict';

require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var _extends = require('@babel/runtime/helpers/extends');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var React = require('react');
var classnames = require('classnames');
var useLangDirection = require('../../utils/useLangDirection.js');
var Button = require('../Button/Button.js');
var Card = require('../Card/Card.js');
var cardUtilityFunctions = require('../../utils/cardUtilityFunctions.js');
var LayoutConstants = require('../../constants/LayoutConstants.js');
var CardPropTypes = require('../../constants/CardPropTypes.js');
var Settings = require('../../constants/Settings.js');
var DragAndDropUtils = require('../../utils/DragAndDropUtils.js');
var Legend = require('./Legend.js');
var ZoomControl = require('./ZoomControl.js');
var MapControls = require('./MapControls.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var _excluded = ["children", "mapContainerRef", "availableActions", "size", "mapControls", "isResizable", "isExpanded", "i18n", "id", "isLegendFullWidth", "onZoomIn", "onZoomOut", "stops", "options", "layeredControls", "onCardAction", "isSettingPanelOpen", "settingsContent", "dropRef", "testID", "testId"];
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), !0).forEach(function (r) { _defineProperty__default.default(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 iotPrefix = Settings.settings.iotPrefix;
var propTypes = _objectSpread(_objectSpread({}, CardPropTypes.CardPropTypes), CardPropTypes.MapCardPropTypes);
var defaultProps = {
  isLegendFullWidth: false,
  isSettingPanelOpen: false,
  i18n: {
    cardTitle: 'Card Title',
    zoomIn: 'Zoom In',
    zoomOut: 'Zoom out',
    configurationTitle: 'Map configuration',
    closeSettingsIconText: 'Close',
    expandLabel: 'Expand',
    layerTriggerIconDescription: 'Layered controls',
    legendTitle: 'Legend',
    hideLegend: 'Hide legend',
    showLegend: 'Show legend',
    scrollUp: 'Scroll up',
    scrollDown: 'Scroll down'
  },
  layeredControls: [],
  mapControls: [],
  size: LayoutConstants.CARD_SIZES.LARGEWIDE,
  stops: [],
  testId: 'map-card'
};
var MapCard = function MapCard(_ref) {
  var children = _ref.children,
    mapContainerRef = _ref.mapContainerRef,
    availableActions = _ref.availableActions,
    size = _ref.size,
    mapControls = _ref.mapControls,
    isResizable = _ref.isResizable,
    isExpanded = _ref.isExpanded,
    i18n = _ref.i18n,
    id = _ref.id,
    isLegendFullWidth = _ref.isLegendFullWidth,
    onZoomIn = _ref.onZoomIn,
    onZoomOut = _ref.onZoomOut,
    stops = _ref.stops;
    _ref.options;
    var layeredControls = _ref.layeredControls,
    onCardAction = _ref.onCardAction,
    isSettingPanelOpen = _ref.isSettingPanelOpen,
    SettingsContent = _ref.settingsContent,
    dropRef = _ref.dropRef,
    testID = _ref.testID,
    testId = _ref.testId,
    others = _objectWithoutProperties__default.default(_ref, _excluded);
  var mergedI18n = React.useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  }, [i18n]);
  var langDir = useLangDirection.useLangDirection();
  // Checks size property against new size naming convention and reassigns to closest supported size if necessary.
  var newSize = cardUtilityFunctions.getUpdatedCardSize(size);
  var BASE_CLASS_NAME = "".concat(iotPrefix, "--map");
  var resizeHandles = React.useMemo(function () {
    return isResizable ? cardUtilityFunctions.getResizeHandles(children) : [];
  }, [children, isResizable]);
  var _useState = React.useState(false),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    isLegendCollapsed = _useState2[0],
    setIsLegendCollapsed = _useState2[1];
  var tooltipPosition = React__default.default.useMemo(function () {
    return langDir === 'ltr' ? 'left' : 'right';
  }, [langDir]);
  var controls = mapControls.length || layeredControls.length ? /*#__PURE__*/React__default.default.createElement(MapControls
  // TODO: remove deprecated 'testID' in v3.
  , {
    testId: "".concat(testID || testId, "-map-controls"),
    controls: mapControls,
    layeredControls: layeredControls,
    tooltipPosition: tooltipPosition,
    i18n: mergedI18n,
    isExpandedMode: isExpanded
  }) : null;
  var myAvailableActions = _objectSpread(_objectSpread({}, availableActions), {}, {
    settings: isExpanded && (availableActions === null || availableActions === void 0 ? void 0 : availableActions.settings)
  });
  return /*#__PURE__*/React__default.default.createElement(Card.default, _extends__default.default({
    title: mergedI18n.cardTitle,
    size: newSize,
    availableActions: myAvailableActions,
    isResizable: isResizable,
    isExpanded: isExpanded,
    resizeHandles: resizeHandles,
    i18n: mergedI18n,
    id: id,
    renderExpandIcon: iconsReact.Maximize,
    onCardAction: onCardAction,
    contentClassName: "".concat(BASE_CLASS_NAME, "-card-content"),
    className: "".concat(BASE_CLASS_NAME)
    // TODO: remove deprecated 'testID' in v3.
    ,
    testId: testID || testId
  }, others), /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement("div", {
    ref: DragAndDropUtils.mergeRefs(mapContainerRef, dropRef),
    className: classnames__default.default("".concat(BASE_CLASS_NAME, "__container"), _defineProperty__default.default({}, "".concat(BASE_CLASS_NAME, "__container--open"), isSettingPanelOpen))
  }, children, /*#__PURE__*/React__default.default.createElement("div", {
    className: classnames__default.default("".concat(BASE_CLASS_NAME, "-controls"), _defineProperty__default.default(_defineProperty__default.default({}, "".concat(BASE_CLASS_NAME, "-controls--has-fullwidth-legend"), isLegendFullWidth && !isLegendCollapsed), "".concat(BASE_CLASS_NAME, "-controls--has-increased-margins"), isExpanded))
  }, controls, /*#__PURE__*/React__default.default.createElement(ZoomControl
  // TODO: remove deprecated 'testID' in v3.
  , {
    testId: "".concat(testID || testId, "-zoom-control"),
    i18n: {
      zoomIn: mergedI18n.zoomIn,
      zoomOut: mergedI18n.zoomOut
    },
    onZoomIn: onZoomIn,
    onZoomOut: onZoomOut,
    tooltipPosition: tooltipPosition,
    smallButtons: !isExpanded
  })), /*#__PURE__*/React__default.default.createElement(Legend
  // TODO: remove deprecated 'testID' in v3.
  , {
    testId: "".concat(testID || testId, "-legend"),
    hideLegendText: mergedI18n.hideLegend,
    showLegendText: mergedI18n.showLegend,
    titleText: mergedI18n.legendTitle,
    stops: stops,
    isFullWidth: isLegendFullWidth,
    isCollapsed: isLegendCollapsed,
    increasedMargin: isExpanded,
    onCollapsToggle: function onCollapsToggle() {
      setIsLegendCollapsed(!isLegendCollapsed);
    }
  })), /*#__PURE__*/React__default.default.createElement("div", {
    "data-testid": "".concat(testID || testId, "-settings"),
    className: classnames__default.default("".concat(BASE_CLASS_NAME, "-settings"), _defineProperty__default.default({}, "".concat(BASE_CLASS_NAME, "-settings--open"), isSettingPanelOpen))
  }, /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(BASE_CLASS_NAME, "-settings-header")
  }, /*#__PURE__*/React__default.default.createElement("h3", {
    className: "".concat(BASE_CLASS_NAME, "-settings-header__title")
  }, mergedI18n.configurationTitle), /*#__PURE__*/React__default.default.createElement(Button, {
    className: "".concat(BASE_CLASS_NAME, "-settings-close-btn"),
    kind: "ghost",
    size: "sm",
    hasIconOnly: true,
    renderIcon: iconsReact.Close,
    iconDescription: mergedI18n.closeSettingsIconText,
    onClick: function onClick() {
      onCardAction(id, LayoutConstants.CARD_ACTIONS.ON_SETTINGS_CLICK);
    }
  })), /*#__PURE__*/React__default.default.createElement(SettingsContent, null))));
};
MapCard.propTypes = propTypes;
MapCard.defaultProps = defaultProps;
MapCard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "MapCard",
  "props": {
    "isLegendFullWidth": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "isSettingPanelOpen": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  cardTitle: 'Card Title',\n  zoomIn: 'Zoom In',\n  zoomOut: 'Zoom out',\n  configurationTitle: 'Map configuration',\n  closeSettingsIconText: 'Close',\n  expandLabel: 'Expand',\n  layerTriggerIconDescription: 'Layered controls',\n  legendTitle: 'Legend',\n  hideLegend: 'Hide legend',\n  showLegend: 'Show legend',\n  scrollUp: 'Scroll up',\n  scrollDown: 'Scroll down',\n}",
        "computed": false
      },
      "required": false
    },
    "layeredControls": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "required": false
    },
    "mapControls": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "required": false
    },
    "size": {
      "defaultValue": {
        "value": "CARD_SIZES.LARGEWIDE",
        "computed": true
      },
      "required": false
    },
    "stops": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'map-card'",
        "computed": false
      },
      "required": false
    }
  },
  "composes": ["../../constants/CardPropTypes"]
};
var MapCard$1 = MapCard;

module.exports = MapCard$1;
