'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var omit = require('../../../../../node_modules/lodash-es/omit.js');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.number.is-nan.js');
require('core-js/modules/es.number.parse-int.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');

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
      })),
      decimalPrecision: PropTypes__default.default.number,
      xLabel: PropTypes__default.default.string,
      yLabel: PropTypes__default.default.string,
      unit: PropTypes__default.default.string,
      includeZeroOnXaxis: PropTypes__default.default.bool,
      includeZeroOnYaxis: PropTypes__default.default.bool,
      timeDataSourceId: PropTypes__default.default.string,
      showLegend: PropTypes__default.default.bool,
      /**
       * maximum amounts of data points to render in the graph
       * The data point limitation doesn't actually affect the cards themselves. It is only stored
       * in the JSON and up to the downstream consumers to use when they fetch their data.
       */
      maximumDataPoints: PropTypes__default.default.number
    }),
    interval: PropTypes__default.default.string
  }),
  /** Callback function when form data changes */
  onChange: PropTypes__default.default.func.isRequired,
  i18n: PropTypes__default.default.shape({
    xAxisLabel: PropTypes__default.default.string,
    yAxisLabel: PropTypes__default.default.string,
    unitLabel: PropTypes__default.default.string,
    decimalPrecisionLabel: PropTypes__default.default.string,
    maximumDataPointsLabel: PropTypes__default.default.string
  })
};
var defaultProps = {
  cardConfig: {},
  i18n: {
    xAxisLabel: 'X-axis label',
    yAxisLabel: 'Y-axis label',
    includeZeroOnXaxis: 'Include zero on x-axis',
    includeZeroOnYaxis: 'Include zero on y-axis',
    unitLabel: 'Unit',
    decimalPrecisionLabel: 'Decimal precision',
    maximumDataPoints: 'Maximum data points'
  }
};
var DataSeriesFormSettings = function DataSeriesFormSettings(_ref) {
  var _content$xLabel, _content$yLabel, _content$unit, _cardConfig$content, _cardConfig$content2;
  var cardConfig = _ref.cardConfig,
    _onChange = _ref.onChange,
    i18n = _ref.i18n;
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var content = cardConfig.content,
    id = cardConfig.id;
  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.TextInput, {
    id: "".concat(id, "_title"),
    labelText: mergedI18n.xAxisLabel,
    light: true,
    onChange: function onChange(evt) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          xLabel: evt.target.value
        })
      }));
    },
    value: (_content$xLabel = content === null || content === void 0 ? void 0 : content.xLabel) !== null && _content$xLabel !== void 0 ? _content$xLabel : ''
  })), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement(react.TextInput, {
    id: "".concat(id, "_y-axis-label"),
    labelText: mergedI18n.yAxisLabel,
    light: true,
    onChange: function onChange(evt) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          yLabel: evt.target.value
        })
      }));
    },
    value: (_content$yLabel = content === null || content === void 0 ? void 0 : content.yLabel) !== null && _content$yLabel !== void 0 ? _content$yLabel : ''
  })), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement(react.TextInput, {
    id: "".concat(id, "_unit-selection"),
    labelText: mergedI18n.unitLabel,
    light: true,
    onChange: function onChange(evt) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          unit: evt.target.value
        })
      }));
    },
    value: (_content$unit = content === null || content === void 0 ? void 0 : content.unit) !== null && _content$unit !== void 0 ? _content$unit : ''
  })), /*#__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, "_decimal-precision"),
    label: mergedI18n.decimalPrecisionLabel,
    min: 0,
    max: 100,
    onChange: function onChange(event, _ref2) {
      var value = _ref2.value;
      var decimalPrecision = Number.parseInt(value, 10);
      _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, omit(cardConfig.content, 'decimalPrecision')), Number.isNaN(decimalPrecision) || decimalPrecision < 0 ? {} : {
          decimalPrecision: decimalPrecision
        })
      }));
    },
    value: content === null || content === void 0 ? void 0 : content.decimalPrecision
  }))), /*#__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, "_maximum_data_points"),
    label: mergedI18n.maximumDataPoints,
    step: 10,
    min: 0,
    onChange: function onChange(event, _ref3) {
      var value = _ref3.value;
      var maximumDataPoints = Number.parseInt(value, 10);
      _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          maximumDataPoints: maximumDataPoints < 0 ? 0 : maximumDataPoints
        })
      }));
    },
    value: content === null || content === void 0 ? void 0 : content.maximumDataPoints
  }))), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input--toggle-field ").concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement("span", null, mergedI18n.includeZeroOnXaxis), /*#__PURE__*/React__default.default.createElement(react.Toggle, {
    size: "sm",
    "data-testid": "includeZeroOnXaxis-toggle",
    id: "includeZeroOnXaxis-toggle",
    "aria-label": mergedI18n.includeZeroOnXaxis,
    labelA: "",
    labelB: "",
    toggled: (_cardConfig$content = cardConfig.content) === null || _cardConfig$content === void 0 ? void 0 : _cardConfig$content.includeZeroOnXaxis,
    onToggle: function onToggle(bool) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          includeZeroOnXaxis: bool
        })
      }));
    }
  })), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input--toggle-field ").concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement("span", null, mergedI18n.includeZeroOnYaxis), /*#__PURE__*/React__default.default.createElement(react.Toggle, {
    size: "sm",
    "data-testid": "includeZeroOnYaxis-toggle",
    id: "includeZeroOnYaxis-toggle",
    "aria-label": mergedI18n.includeZeroOnYaxis,
    labelA: "",
    labelB: "",
    toggled: (_cardConfig$content2 = cardConfig.content) === null || _cardConfig$content2 === void 0 ? void 0 : _cardConfig$content2.includeZeroOnYaxis,
    onToggle: function onToggle(bool) {
      return _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          includeZeroOnYaxis: bool
        })
      }));
    }
  })));
};
DataSeriesFormSettings.propTypes = propTypes;
DataSeriesFormSettings.defaultProps = defaultProps;
DataSeriesFormSettings.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "DataSeriesFormSettings",
  "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
              },
              "decimalPrecision": {
                "name": "number",
                "required": false
              },
              "xLabel": {
                "name": "string",
                "required": false
              },
              "yLabel": {
                "name": "string",
                "required": false
              },
              "unit": {
                "name": "string",
                "required": false
              },
              "includeZeroOnXaxis": {
                "name": "bool",
                "required": false
              },
              "includeZeroOnYaxis": {
                "name": "bool",
                "required": false
              },
              "timeDataSourceId": {
                "name": "string",
                "required": false
              },
              "showLegend": {
                "name": "bool",
                "required": false
              },
              "maximumDataPoints": {
                "name": "number",
                "description": "maximum amounts of data points to render in the graph\nThe data point limitation doesn't actually affect the cards themselves. It is only stored\nin the JSON and up to the downstream consumers to use when they fetch their data.",
                "required": false
              }
            },
            "required": false
          },
          "interval": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  xAxisLabel: 'X-axis label',\n  yAxisLabel: 'Y-axis label',\n  includeZeroOnXaxis: 'Include zero on x-axis',\n  includeZeroOnYaxis: 'Include zero on y-axis',\n  unitLabel: 'Unit',\n  decimalPrecisionLabel: 'Decimal precision',\n  maximumDataPoints: 'Maximum data points',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "xAxisLabel": {
            "name": "string",
            "required": false
          },
          "yAxisLabel": {
            "name": "string",
            "required": false
          },
          "unitLabel": {
            "name": "string",
            "required": false
          },
          "decimalPrecisionLabel": {
            "name": "string",
            "required": false
          },
          "maximumDataPointsLabel": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "onChange": {
      "description": "Callback function when form data changes",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var DataSeriesFormSettings$1 = DataSeriesFormSettings;

module.exports = DataSeriesFormSettings$1;
