import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.filter.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 _extends from '@babel/runtime/helpers/extends';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _merge from '../../../node_modules/lodash-es/merge.js';
import React__default, { useState } from 'react';
import PropTypes from 'prop-types';
import ComposedModal from '../../ComposedModal/ComposedModal.js';
import { settings } from '../../../constants/Settings.js';
import { OverridePropTypes } from '../../../constants/SharedPropTypes.js';
import deprecate from '../../../internal/deprecate.js';
import TableSaveViewForm from './TableSaveViewForm.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 = {
  /**
   * Callbacks for the actions of the modal
   * onSave : Called with { title: string, defaultView: boolean, publicView: boolean} on save button click
   * onClose : Called on cancel button click and on the top right close icon click
   * onClearError : Called when the error msg is cleared
   * onChange : Called when view title input value is changed
   */
  actions: PropTypes.shape({
    onSave: PropTypes.func,
    onClose: PropTypes.func,
    onClearError: PropTypes.func,
    onChange: PropTypes.func
  }).isRequired,
  /** Shows this string as a general modal error when present */
  error: PropTypes.string,
  /** Internationalisation strings object */
  i18n: PropTypes.shape({
    modalTitle: PropTypes.string,
    modalBodyText: PropTypes.string,
    titleInputLabelText: PropTypes.string,
    defaultCheckboxLabelText: PropTypes.string,
    publicCheckboxLabelText: PropTypes.string,
    closeIconDescription: PropTypes.string,
    saveButtonLabelText: PropTypes.string,
    cancelButtonLabelText: PropTypes.string
  }),
  /**
   * The initial values of the form elements e.g: { title: 'My view 1', isDefault: true }.
   * Can be extended to contain any key-value pair needed for a custom form.
   */
  initialFormValues: PropTypes.oneOfType([PropTypes.shape({
    title: PropTypes.string,
    isDefault: PropTypes.bool,
    isPublic: PropTypes.bool
  }), PropTypes.object]),
  /** Determines if the modal is open or closed (i.e. visible or not to the user) */
  open: PropTypes.bool.isRequired,
  /** Used to overide the internal components and props for advanced customisation */
  overrides: PropTypes.shape({
    composedModal: OverridePropTypes,
    tableSaveViewForm: OverridePropTypes
  }),
  /** Disables the form and shows spinner on save button when true */
  sendingData: PropTypes.bool,
  // TODO: remove deprecated 'testID' in v3
  // eslint-disable-next-line react/require-default-props
  testID: deprecate(PropTypes.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  /** Id that can be used for testing */
  testId: PropTypes.string,
  /** When true it will show that the title field has invalid input */
  titleInputInvalid: PropTypes.bool,
  /** Used to describe the input validation error for the the title field */
  titleInputInvalidText: PropTypes.string,
  /** A string that describes what this view contains */
  viewDescription: PropTypes.string
};
var defaultProps = {
  error: undefined,
  i18n: {
    modalTitle: 'Save new view',
    modalBodyText: "You can save the current view's settings including applied filters and search.",
    titleInputLabelText: 'View title',
    defaultCheckboxLabelText: 'Save as my default view',
    publicCheckboxLabelText: 'Public view',
    closeIconDescription: 'Close',
    saveButtonLabelText: 'Save',
    cancelButtonLabelText: 'Cancel'
  },
  initialFormValues: {
    title: '',
    isDefault: false,
    isPublic: false
  },
  overrides: undefined,
  sendingData: false,
  testId: 'TableSaveViewModal',
  titleInputInvalid: false,
  titleInputInvalidText: undefined,
  viewDescription: undefined
};
var TableSaveViewModal = function TableSaveViewModal(_ref) {
  var _overrides$composedMo, _overrides$tableSaveV, _overrides$composedMo2, _overrides$tableSaveV2;
  var _ref$actions = _ref.actions,
    onSaveCallback = _ref$actions.onSave,
    onClose = _ref$actions.onClose,
    onClearError = _ref$actions.onClearError,
    onChangeCallback = _ref$actions.onChange,
    error = _ref.error,
    i18n = _ref.i18n,
    initialFormValues = _ref.initialFormValues,
    open = _ref.open,
    overrides = _ref.overrides,
    sendingData = _ref.sendingData,
    testID = _ref.testID,
    testId = _ref.testId,
    titleInputInvalid = _ref.titleInputInvalid,
    titleInputInvalidText = _ref.titleInputInvalidText,
    viewDescription = _ref.viewDescription;
  var _useState = useState(initialFormValues),
    _useState2 = _slicedToArray(_useState, 2),
    formValues = _useState2[0],
    setFormValues = _useState2[1];
  var _merge2 = _merge({}, defaultProps.i18n, i18n),
    modalTitle = _merge2.modalTitle,
    modalBodyText = _merge2.modalBodyText,
    titleInputLabelText = _merge2.titleInputLabelText,
    defaultCheckboxLabelText = _merge2.defaultCheckboxLabelText,
    publicCheckboxLabelText = _merge2.publicCheckboxLabelText,
    closeIconDescription = _merge2.closeIconDescription,
    saveButtonLabelText = _merge2.saveButtonLabelText,
    cancelButtonLabelText = _merge2.cancelButtonLabelText;
  var onSave = function onSave() {
    return onSaveCallback(_objectSpread(_objectSpread({}, formValues), {}, {
      description: viewDescription
    }));
  };
  var onChange = function onChange(modifiedFormValue) {
    setFormValues(function (currentValues) {
      return _objectSpread(_objectSpread({}, currentValues), modifiedFormValue);
    });
    onChangeCallback(modifiedFormValue);
  };
  var primaryInputId = 'save-view-modal-view-title';
  var i18nFooter = {
    primaryButtonLabel: saveButtonLabelText,
    secondaryButtonLabel: cancelButtonLabelText
  };
  var MyComposedModal = (overrides === null || overrides === void 0 || (_overrides$composedMo = overrides.composedModal) === null || _overrides$composedMo === void 0 ? void 0 : _overrides$composedMo.component) || ComposedModal;
  var MyTableSaveViewForm = (overrides === null || overrides === void 0 || (_overrides$tableSaveV = overrides.tableSaveViewForm) === null || _overrides$tableSaveV === void 0 ? void 0 : _overrides$tableSaveV.component) || TableSaveViewForm;
  return /*#__PURE__*/React__default.createElement(MyComposedModal, _extends({
    footer: _objectSpread(_objectSpread({}, i18nFooter), {}, {
      isPrimaryButtonDisabled: formValues.title === ''
    }),
    iconDescription: closeIconDescription
    // TODO: remove deprecated 'testID' in v3
    ,
    testID: testID || testId,
    error: error,
    header: {
      title: modalTitle
    },
    onClearError: onClearError,
    onClose: onClose,
    onSubmit: onSave,
    open: open,
    sendingData: sendingData,
    selectorPrimaryFocus: "#".concat(primaryInputId)
  }, overrides === null || overrides === void 0 || (_overrides$composedMo2 = overrides.composedModal) === null || _overrides$composedMo2 === void 0 ? void 0 : _overrides$composedMo2.props), /*#__PURE__*/React__default.createElement("p", {
    className: "".concat(iotPrefix, "--save-view-modal__body-text")
  }, modalBodyText), /*#__PURE__*/React__default.createElement(MyTableSaveViewForm, _extends({
    viewDescription: viewDescription,
    titleInputInvalid: titleInputInvalid,
    titleInputInvalidText: titleInputInvalidText,
    onChange: onChange,
    formValues: formValues,
    disabled: sendingData,
    primaryInputId: primaryInputId,
    i18n: {
      titleInputLabelText: titleInputLabelText,
      defaultCheckboxLabelText: defaultCheckboxLabelText,
      publicCheckboxLabelText: publicCheckboxLabelText
    }
    // TODO: remove deprecated 'testID' in v3
    ,
    testID: "".concat(testID || testId, "-form")
  }, overrides === null || overrides === void 0 || (_overrides$tableSaveV2 = overrides.tableSaveViewForm) === null || _overrides$tableSaveV2 === void 0 ? void 0 : _overrides$tableSaveV2.props)));
};
TableSaveViewModal.propTypes = propTypes;
TableSaveViewModal.defaultProps = defaultProps;
TableSaveViewModal.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TableSaveViewModal",
  "props": {
    "error": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Shows this string as a general modal error when present",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  modalTitle: 'Save new view',\n  modalBodyText: `You can save the current view's settings including applied filters and search.`,\n  titleInputLabelText: 'View title',\n  defaultCheckboxLabelText: 'Save as my default view',\n  publicCheckboxLabelText: 'Public view',\n  closeIconDescription: 'Close',\n  saveButtonLabelText: 'Save',\n  cancelButtonLabelText: 'Cancel',\n}",
        "computed": false
      },
      "description": "Internationalisation strings object",
      "type": {
        "name": "shape",
        "value": {
          "modalTitle": {
            "name": "string",
            "required": false
          },
          "modalBodyText": {
            "name": "string",
            "required": false
          },
          "titleInputLabelText": {
            "name": "string",
            "required": false
          },
          "defaultCheckboxLabelText": {
            "name": "string",
            "required": false
          },
          "publicCheckboxLabelText": {
            "name": "string",
            "required": false
          },
          "closeIconDescription": {
            "name": "string",
            "required": false
          },
          "saveButtonLabelText": {
            "name": "string",
            "required": false
          },
          "cancelButtonLabelText": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "initialFormValues": {
      "defaultValue": {
        "value": "{\n  title: '',\n  isDefault: false,\n  isPublic: false,\n}",
        "computed": false
      },
      "description": "The initial values of the form elements e.g: { title: 'My view 1', isDefault: true }.\nCan be extended to contain any key-value pair needed for a custom form.",
      "type": {
        "name": "union",
        "value": [{
          "name": "shape",
          "value": {
            "title": {
              "name": "string",
              "required": false
            },
            "isDefault": {
              "name": "bool",
              "required": false
            },
            "isPublic": {
              "name": "bool",
              "required": false
            }
          }
        }, {
          "name": "object"
        }]
      },
      "required": false
    },
    "overrides": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Used to overide the internal components and props for advanced customisation",
      "type": {
        "name": "shape",
        "value": {
          "composedModal": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "tableSaveViewForm": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          }
        }
      },
      "required": false
    },
    "sendingData": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Disables the form and shows spinner on save button when true",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'TableSaveViewModal'",
        "computed": false
      },
      "description": "Id that can be used for testing",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "titleInputInvalid": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "When true it will show that the title field has invalid input",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "titleInputInvalidText": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Used to describe the input validation error for the the title field",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "viewDescription": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "A string that describes what this view contains",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "actions": {
      "description": "Callbacks for the actions of the modal\nonSave : Called with { title: string, defaultView: boolean, publicView: boolean} on save button click\nonClose : Called on cancel button click and on the top right close icon click\nonClearError : Called when the error msg is cleared\nonChange : Called when view title input value is changed",
      "type": {
        "name": "shape",
        "value": {
          "onSave": {
            "name": "func",
            "required": false
          },
          "onClose": {
            "name": "func",
            "required": false
          },
          "onClearError": {
            "name": "func",
            "required": false
          },
          "onChange": {
            "name": "func",
            "required": false
          }
        }
      },
      "required": true
    },
    "open": {
      "description": "Determines if the modal is open or closed (i.e. visible or not to the user)",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "testID": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "deprecate(\n  PropTypes.string,\n  `The 'testID' prop has been deprecated. Please use 'testId' instead.`\n)"
      },
      "required": false
    }
  }
};
var TableSaveViewModal$1 = TableSaveViewModal;

export { TableSaveViewModal$1 as default };
