import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default, { useMemo, useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import { CARD_TYPES } from '../../../constants/LayoutConstants.js';
import { DataItemsPropTypes, DashboardEditorActionsPropTypes, defaultDashboardEditorActionsProps } from '../../DashboardEditor/editorUtils.js';
import { settings } from '../../../constants/Settings.js';
import CommonCardEditFormFields from './CommonCardEditFormFields.js';
import DataSeriesFormContent from './CardEditFormItems/DataSeriesFormItems/DataSeriesFormContent.js';
import ImageCardFormContent from './CardEditFormItems/ImageCardFormItems/ImageCardFormContent.js';
import TableCardFormContent from './CardEditFormItems/TableCardFormItems/TableCardFormContent.js';
import ContentFormItemTitle from './CardEditFormItems/ContentFormItemTitle.js';

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(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.iotPrefix;
var propTypes = {
  /** card data value */
  cardConfig: PropTypes.shape({
    id: PropTypes.string,
    title: PropTypes.string,
    size: PropTypes.string,
    type: PropTypes.string,
    timeRange: PropTypes.string,
    renderEditContent: PropTypes.func,
    content: PropTypes.oneOfType([PropTypes.shape({
      series: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string,
        dataSourceId: PropTypes.string,
        color: PropTypes.string
      })),
      xLabel: PropTypes.string,
      yLabel: PropTypes.string,
      unit: PropTypes.string,
      includeZeroOnXaxis: PropTypes.bool,
      includeZeroOnYaxis: PropTypes.bool,
      timeDataSourceId: PropTypes.string,
      showLegend: PropTypes.bool
    }), PropTypes.shape({
      id: PropTypes.string,
      src: PropTypes.string,
      zoomMax: PropTypes.number
    }),
    // custom card content is a function
    PropTypes.func]),
    dataSource: PropTypes.shape({
      attributes: PropTypes.arrayOf(PropTypes.shape({
        aggregator: PropTypes.string,
        attribute: PropTypes.string,
        id: PropTypes.string
      }))
    })
  }),
  /** Callback function when form data changes */
  onChange: PropTypes.func.isRequired,
  i18n: PropTypes.shape({
    cardTitle: PropTypes.string,
    description: PropTypes.string,
    size: PropTypes.string,
    selectASize: PropTypes.string,
    timeRange: PropTypes.string,
    selectATimeRange: PropTypes.string,
    last24HoursLabel: PropTypes.string,
    last7DaysLabel: PropTypes.string,
    lastMonthLabel: PropTypes.string,
    lastQuarterLabel: PropTypes.string,
    lastYearLabel: PropTypes.string,
    thisWeekLabel: PropTypes.string,
    thisMonthLabel: PropTypes.string,
    thisQuarterLabel: PropTypes.string,
    thisYearLabel: PropTypes.string
  }),
  /** whether to use translated labels in cards */
  shouldUseTranslatedLabels: PropTypes.bool,
  /** if provided, returns an array of strings which are the timeRanges to be allowed
   * on each card
   * getValidTimeRanges(card, selectedDataItems)
   */
  getValidTimeRanges: PropTypes.func,
  /** if provided, returns an array of strings which are the dataItems to be allowed
   * on each card
   * getValidDataItems(card, selectedTimeRange)
   */
  getValidDataItems: PropTypes.func,
  /** an array of dataItems to be included on each card
   * this prop will be ignored if getValidDataItems is defined
   */
  dataItems: DataItemsPropTypes,
  currentBreakpoint: PropTypes.string,
  /** an object where the keys are available dimensions and the values are the values available for those dimensions
   *  ex: { manufacturer: ['Rentech', 'GHI Industries'], deviceid: ['73000', '73001', '73002'] }
   */
  availableDimensions: PropTypes.shape({}),
  isSummaryDashboard: PropTypes.bool,
  /** optional link href's for each card type that will appear in a tooltip */
  dataSeriesItemLinks: PropTypes.shape({
    simpleBar: PropTypes.string,
    groupedBar: PropTypes.string,
    stackedBar: PropTypes.string,
    timeSeries: PropTypes.string,
    value: PropTypes.string,
    custom: PropTypes.string,
    table: PropTypes.string,
    image: PropTypes.string
  }),
  actions: DashboardEditorActionsPropTypes
};
var defaultProps = {
  cardConfig: {},
  i18n: {},
  shouldUseTranslatedLabels: false,
  getValidDataItems: null,
  getValidTimeRanges: null,
  dataItems: [],
  currentBreakpoint: 'xl',
  availableDimensions: {},
  isSummaryDashboard: false,
  dataSeriesItemLinks: null,
  actions: defaultDashboardEditorActionsProps
};
var handleTranslationCallback = function handleTranslationCallback(idToTranslate, mergedI18n) {
  var openMenuText = mergedI18n.openMenuText,
    closeMenuText = mergedI18n.closeMenuText,
    clearAllText = mergedI18n.clearAllText;
  switch (idToTranslate) {
    case 'clear.all':
      return clearAllText || 'Clear all';
    case 'open.menu':
      return openMenuText || 'Open menu';
    case 'close.menu':
      return closeMenuText || 'Close menu';
    default:
      return '';
  }
};
var CardEditFormContent = function CardEditFormContent(_ref) {
  var cardConfig = _ref.cardConfig,
    isSummaryDashboard = _ref.isSummaryDashboard,
    onChange = _ref.onChange,
    i18n = _ref.i18n,
    shouldUseTranslatedLabels = _ref.shouldUseTranslatedLabels,
    dataItems = _ref.dataItems,
    getValidDataItems = _ref.getValidDataItems,
    getValidTimeRanges = _ref.getValidTimeRanges,
    currentBreakpoint = _ref.currentBreakpoint,
    availableDimensions = _ref.availableDimensions,
    dataSeriesItemLinks = _ref.dataSeriesItemLinks,
    onFetchDynamicDemoHotspots = _ref.onFetchDynamicDemoHotspots,
    actions = _ref.actions;
  var type = cardConfig.type,
    timeRange = cardConfig.timeRange,
    renderEditContent = cardConfig.renderEditContent;
  var mergedI18n = useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  }, [i18n]);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selectedDataItems = _useState2[0],
    setSelectedDataItems = _useState2[1];
  var _useState3 = useState(timeRange || ''),
    _useState4 = _slicedToArray(_useState3, 2),
    selectedTimeRange = _useState4[0],
    setSelectedTimeRange = _useState4[1];
  var handleTranslation = useCallback(function (idToTranslate) {
    return handleTranslationCallback(idToTranslate, mergedI18n);
  }, [mergedI18n]);
  var editContentSections = renderEditContent && renderEditContent(onChange, cardConfig);
  return /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--card-edit-form--content")
  }, /*#__PURE__*/React__default.createElement(CommonCardEditFormFields, {
    cardConfig: cardConfig,
    key: "".concat(cardConfig.id, "-common") // fix because I need to regenerate the form state when switching between cards
    ,
    onChange: onChange,
    i18n: mergedI18n,
    shouldUseTranslatedLabels: shouldUseTranslatedLabels,
    getValidTimeRanges: getValidTimeRanges,
    currentBreakpoint: currentBreakpoint,
    selectedDataItems: selectedDataItems,
    setSelectedTimeRange: setSelectedTimeRange,
    translateWithId: handleTranslation
  }), type === CARD_TYPES.IMAGE ? /*#__PURE__*/React__default.createElement(ImageCardFormContent, {
    cardConfig: cardConfig,
    key: "".concat(cardConfig.id, "-image") // fix because I need to regenerate the form state when switching between cards
    ,
    i18n: mergedI18n,
    onChange: onChange,
    dataSeriesItemLinks: dataSeriesItemLinks,
    dataItems: dataItems,
    getValidDataItems: getValidDataItems,
    availableDimensions: availableDimensions,
    translateWithId: handleTranslation,
    onFetchDynamicDemoHotspots: onFetchDynamicDemoHotspots,
    actions: actions
  }) : type === CARD_TYPES.TABLE ? /*#__PURE__*/React__default.createElement(TableCardFormContent, {
    cardConfig: cardConfig,
    key: "".concat(cardConfig.id, "-table") // fix because I need to regenerate the form state when switching between cards
    ,
    i18n: mergedI18n,
    onChange: onChange,
    dataItems: dataItems,
    selectedDataItems: selectedDataItems,
    selectedTimeRange: selectedTimeRange,
    setSelectedDataItems: setSelectedDataItems,
    getValidDataItems: getValidDataItems,
    availableDimensions: availableDimensions,
    dataSeriesItemLinks: dataSeriesItemLinks,
    translateWithId: handleTranslation,
    actions: actions
  }) : type === CARD_TYPES.BAR || type === CARD_TYPES.TIMESERIES || type === CARD_TYPES.VALUE || type === CARD_TYPES.LIST ? /*#__PURE__*/React__default.createElement(DataSeriesFormContent, {
    cardConfig: cardConfig,
    isSummaryDashboard: isSummaryDashboard,
    key: "".concat(cardConfig.id, "-data") // fix because I need to regenerate the form state when switching between cards
    ,
    onChange: onChange,
    dataItems: dataItems,
    selectedDataItems: selectedDataItems,
    setSelectedDataItems: setSelectedDataItems,
    selectedTimeRange: selectedTimeRange,
    getValidDataItems: getValidDataItems,
    availableDimensions: availableDimensions,
    i18n: mergedI18n,
    shouldUseTranslatedLabels: shouldUseTranslatedLabels,
    dataSeriesItemLinks: dataSeriesItemLinks,
    translateWithId: handleTranslation,
    actions: actions
  }) : null, Array.isArray(editContentSections) // render the content sections for all types of card if set
  ? editContentSections.map(function (_ref2, index) {
    var header = _ref2.header,
      content = _ref2.content;
    var _ref3 = header || {},
      title = _ref3.title,
      tooltip = _ref3.tooltip;
    return /*#__PURE__*/React__default.createElement(React__default.Fragment, {
      key: "custom-content-section-".concat(index)
    }, title || tooltip ? /*#__PURE__*/React__default.createElement(ContentFormItemTitle, {
      title: title,
      tooltip: tooltip
    }) : null, content);
  }) : null);
};
CardEditFormContent.propTypes = propTypes;
CardEditFormContent.defaultProps = defaultProps;
CardEditFormContent.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "CardEditFormContent",
  "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
          },
          "timeRange": {
            "name": "string",
            "required": false
          },
          "renderEditContent": {
            "name": "func",
            "required": false
          },
          "content": {
            "name": "union",
            "value": [{
              "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
                },
                "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
                }
              }
            }, {
              "name": "shape",
              "value": {
                "id": {
                  "name": "string",
                  "required": false
                },
                "src": {
                  "name": "string",
                  "required": false
                },
                "zoomMax": {
                  "name": "number",
                  "required": false
                }
              }
            }, {
              "name": "func"
            }],
            "required": false
          },
          "dataSource": {
            "name": "shape",
            "value": {
              "attributes": {
                "name": "arrayOf",
                "value": {
                  "name": "shape",
                  "value": {
                    "aggregator": {
                      "name": "string",
                      "required": false
                    },
                    "attribute": {
                      "name": "string",
                      "required": false
                    },
                    "id": {
                      "name": "string",
                      "required": false
                    }
                  }
                },
                "required": false
              }
            },
            "required": false
          }
        }
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "cardTitle": {
            "name": "string",
            "required": false
          },
          "description": {
            "name": "string",
            "required": false
          },
          "size": {
            "name": "string",
            "required": false
          },
          "selectASize": {
            "name": "string",
            "required": false
          },
          "timeRange": {
            "name": "string",
            "required": false
          },
          "selectATimeRange": {
            "name": "string",
            "required": false
          },
          "last24HoursLabel": {
            "name": "string",
            "required": false
          },
          "last7DaysLabel": {
            "name": "string",
            "required": false
          },
          "lastMonthLabel": {
            "name": "string",
            "required": false
          },
          "lastQuarterLabel": {
            "name": "string",
            "required": false
          },
          "lastYearLabel": {
            "name": "string",
            "required": false
          },
          "thisWeekLabel": {
            "name": "string",
            "required": false
          },
          "thisMonthLabel": {
            "name": "string",
            "required": false
          },
          "thisQuarterLabel": {
            "name": "string",
            "required": false
          },
          "thisYearLabel": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "shouldUseTranslatedLabels": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "whether to use translated labels in cards",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "getValidDataItems": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "if provided, returns an array of strings which are the dataItems to be allowed\non each card\ngetValidDataItems(card, selectedTimeRange)",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "getValidTimeRanges": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "if provided, returns an array of strings which are the timeRanges to be allowed\non each card\ngetValidTimeRanges(card, selectedDataItems)",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "dataItems": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "an array of dataItems to be included on each card\nthis prop will be ignored if getValidDataItems is defined",
      "type": {
        "name": "custom",
        "raw": "DataItemsPropTypes"
      },
      "required": false
    },
    "currentBreakpoint": {
      "defaultValue": {
        "value": "'xl'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "availableDimensions": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "an object where the keys are available dimensions and the values are the values available for those dimensions\n ex: { manufacturer: ['Rentech', 'GHI Industries'], deviceid: ['73000', '73001', '73002'] }",
      "type": {
        "name": "shape",
        "value": {}
      },
      "required": false
    },
    "isSummaryDashboard": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "dataSeriesItemLinks": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "optional link href's for each card type that will appear in a tooltip",
      "type": {
        "name": "shape",
        "value": {
          "simpleBar": {
            "name": "string",
            "required": false
          },
          "groupedBar": {
            "name": "string",
            "required": false
          },
          "stackedBar": {
            "name": "string",
            "required": false
          },
          "timeSeries": {
            "name": "string",
            "required": false
          },
          "value": {
            "name": "string",
            "required": false
          },
          "custom": {
            "name": "string",
            "required": false
          },
          "table": {
            "name": "string",
            "required": false
          },
          "image": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "actions": {
      "defaultValue": {
        "value": "defaultDashboardEditorActionsProps",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "custom",
        "raw": "DashboardEditorActionsPropTypes"
      },
      "required": false
    },
    "onChange": {
      "description": "Callback function when form data changes",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var CardEditFormContent$1 = CardEditFormContent;

export { CardEditFormContent$1 as default, handleTranslationCallback };
