'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.number.constructor.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 React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var Settings = require('../../../../../constants/Settings.js');
var index = require('../../../../Tooltip/index.js');
var valueCardUtils = require('../../../../ValueCard/valueCardUtils.js');
var componentUtilityFunctions = require('../../../../../utils/componentUtilityFunctions.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

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 = {
  /** card data value */
  cardConfig: PropTypes__default.default.shape({
    id: PropTypes__default.default.string,
    title: PropTypes__default.default.string,
    size: PropTypes__default.default.string,
    type: PropTypes__default.default.string,
    content: PropTypes__default.default.shape({
      series: PropTypes__default.default.arrayOf(PropTypes__default.default.shape({
        label: PropTypes__default.default.string,
        dataSourceId: PropTypes__default.default.string,
        color: PropTypes__default.default.string
      })),
      unit: PropTypes__default.default.string
    }),
    interval: PropTypes__default.default.string,
    fontSize: PropTypes__default.default.number,
    isNumberValueCompact: PropTypes__default.default.bool
  }),
  /** Callback function when form data changes */
  onChange: PropTypes__default.default.func.isRequired,
  i18n: PropTypes__default.default.shape({
    fontSize: PropTypes__default.default.string,
    abbreviateNumbers: PropTypes__default.default.string,
    abbreviateNumbersTooltip: PropTypes__default.default.string
  })
};
var defaultProps = {
  cardConfig: {},
  i18n: {
    fontSize: 'Font size',
    abbreviateNumbers: 'Abbreviate numbers',
    abbreviateNumbersTooltip: 'Shorten numbers by using K, M, B. For example, shows "1.2K" instead of "1,200"'
  }
};
var MIN_FONT_SIZE = 16;
var MAX_FONT_SIZE = 54;
var ValueCardFormSettings = function ValueCardFormSettings(_ref) {
  var cardConfig = _ref.cardConfig,
    _onChange = _ref.onChange,
    i18n = _ref.i18n;
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var id = cardConfig.id,
    fontSize = cardConfig.fontSize,
    isNumberValueCompact = cardConfig.isNumberValueCompact;
  var baseClassName = "".concat(iotPrefix, "--card-edit-form");
  return /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement(react.Layer, null, /*#__PURE__*/React__default.default.createElement(react.NumberInput, {
    id: "".concat(id, "_value-card-font-size"),
    step: 1,
    min: MIN_FONT_SIZE,
    max: MAX_FONT_SIZE,
    label: mergedI18n.fontSize,
    value: fontSize || valueCardUtils.DEFAULT_FONT_SIZE,
    onChange: function onChange(event, _ref2) {
      var value = _ref2.value;
      var parsedValue = Number(value !== null && value !== void 0 ? value : event.imaginaryTarget.value);
      if (componentUtilityFunctions.isNumberValidForMinMax(parsedValue, MIN_FONT_SIZE, MAX_FONT_SIZE)) {
        _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
          fontSize: parsedValue
        }));
      }
    }
  }))), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input--toggle-field")
  }, /*#__PURE__*/React__default.default.createElement("span", null, mergedI18n.abbreviateNumbers), /*#__PURE__*/React__default.default.createElement(index.Tooltip, {
    direction: "left",
    triggerText: null,
    triggerId: "".concat(id, "-abbreviate-numbers-tooltip"),
    useAutoPositioning: true
  }, mergedI18n.abbreviateNumbersTooltip), /*#__PURE__*/React__default.default.createElement(react.Toggle, {
    size: "sm",
    id: "".concat(id, "_value-card-number-compact"),
    labelA: "",
    labelB: "",
    toggled: isNumberValueCompact || false,
    onToggle: function onToggle(toggled) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        isNumberValueCompact: toggled
      }));
    },
    "aria-label": "Abbreviate number"
  }))));
};
ValueCardFormSettings.propTypes = propTypes;
ValueCardFormSettings.defaultProps = defaultProps;
ValueCardFormSettings.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ValueCardFormSettings",
  "props": {
    "cardConfig": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "card data value",
      "type": {
        "name": "shape",
        "value": {
          "id": {
            "name": "string",
            "required": false
          },
          "title": {
            "name": "string",
            "required": false
          },
          "size": {
            "name": "string",
            "required": false
          },
          "type": {
            "name": "string",
            "required": false
          },
          "content": {
            "name": "shape",
            "value": {
              "series": {
                "name": "arrayOf",
                "value": {
                  "name": "shape",
                  "value": {
                    "label": {
                      "name": "string",
                      "required": false
                    },
                    "dataSourceId": {
                      "name": "string",
                      "required": false
                    },
                    "color": {
                      "name": "string",
                      "required": false
                    }
                  }
                },
                "required": false
              },
              "unit": {
                "name": "string",
                "required": false
              }
            },
            "required": false
          },
          "interval": {
            "name": "string",
            "required": false
          },
          "fontSize": {
            "name": "number",
            "required": false
          },
          "isNumberValueCompact": {
            "name": "bool",
            "required": false
          }
        }
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  fontSize: 'Font size',\n  abbreviateNumbers: 'Abbreviate numbers',\n  abbreviateNumbersTooltip:\n    'Shorten numbers by using K, M, B. For example, shows \"1.2K\" instead of \"1,200\"',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "fontSize": {
            "name": "string",
            "required": false
          },
          "abbreviateNumbers": {
            "name": "string",
            "required": false
          },
          "abbreviateNumbersTooltip": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "onChange": {
      "description": "Callback function when form data changes",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var ValueCardFormSettings$1 = ValueCardFormSettings;

module.exports = ValueCardFormSettings$1;
