import _extends from '@babel/runtime/helpers/extends';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import 'core-js/modules/es.array.find.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.regexp.exec.js';
import 'core-js/modules/es.string.replace.js';
import 'core-js/modules/es.string.search.js';
import React__default, { useState } from 'react';
import PropTypes from 'prop-types';
import { Search, Checkbox, Modal } from '@carbon/react';
import ComposedModal from '../../ComposedModal/ComposedModal.js';
import { settings } from '../../../constants/Settings.js';
import { OverridePropTypes } from '../../../constants/SharedPropTypes.js';
import { SimplePaginationPropTypes } from '../../SimplePagination/SimplePagination.js';
import deprecate from '../../../internal/deprecate.js';
import useMerged from '../../../hooks/useMerged.js';
import TableManageViewsList from './TableManageViewsList.js';
import { ViewsPropType } from './SharedTableManageViewsModalPropTypes.js';

var _excluded = ["closeIconDescription", "deleteWarningTextTemplate", "deleteWarningCancel", "deleteWarningConfirm", "modalTitle", "publicCheckboxLabelText", "searchPlaceholderText", "searchClearButtonLabelText", "searchIconLabelText"];
var iotPrefix = settings.iotPrefix;
var propTypes = {
  /**
   * Callbacks for the actions of the modal
   * onEdit : Called when the user clicks edit on a view row. The ID of the view is passed as argument.
   * onDelete : Called when the user clicks delete on a view row. The ID of the view is passed as argument.
   * onClearError : Called when the dialog error msg is cleared
   * onSearchChange : Called when the search input value is changed, the search term is passed as argument.
   * onDisplayPublicChange : Called when view title input value is changed, a bool value is passed as argument.
   */
  actions: PropTypes.shape({
    onClose: PropTypes.func,
    onEdit: PropTypes.func,
    onDelete: PropTypes.func,
    onClearError: PropTypes.func,
    onSearchChange: PropTypes.func.isRequired,
    onDisplayPublicChange: PropTypes.func.isRequired
  }).isRequired,
  /** The ID of the view that should be marked as default if any */
  defaultViewId: PropTypes.string,
  /** True means that the 'display public' checkbox is checked by default */
  displayPublicDefaultChecked: PropTypes.bool,
  /** Shows this string as a general modal error when present */
  error: PropTypes.string,
  /** Internationalization strings object */
  i18n: PropTypes.shape({
    closeIconDescription: PropTypes.string,
    defaultLabelText: PropTypes.string,
    deleteIconText: PropTypes.string,
    deleteWarningTextTemplate: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
    deleteWarningCancel: PropTypes.string,
    deleteWarningConfirm: PropTypes.string,
    editIconText: PropTypes.string,
    listTitle: PropTypes.string,
    modalTitle: PropTypes.string,
    privateLabelText: PropTypes.string,
    publicLabelText: PropTypes.string,
    publicCheckboxLabelText: PropTypes.string,
    searchPlaceholderText: PropTypes.string,
    searchClearButtonLabelText: PropTypes.string,
    searchIconLabelText: PropTypes.string
  }),
  /** Shows loading skeleton and disables pagination when true */
  isLoading: PropTypes.bool,
  /** The default value of the search field value */
  searchValueDefault: PropTypes.string,
  /** Determines if the modal is open or closed (i.e. visible or not to the user) */
  open: PropTypes.bool.isRequired,
  /** Used to override the internal components and props for advanced customization */
  overrides: PropTypes.shape({
    mainModal: OverridePropTypes,
    publicCheckbox: OverridePropTypes,
    search: OverridePropTypes,
    tableManageViewsList: OverridePropTypes,
    warningModal: OverridePropTypes
  }),
  /** pagination at the bottom of list, see SimplePaginationPropTypes */
  pagination: PropTypes.shape(SimplePaginationPropTypes),
  // 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,
  /** The views to be displayed in the list, see ViewsPropType */
  views: ViewsPropType.isRequired
};
var defaultProps = {
  defaultViewId: undefined,
  displayPublicDefaultChecked: true,
  error: undefined,
  i18n: {
    closeIconDescription: 'Close',
    defaultLabelText: 'default',
    deleteIconText: 'delete',
    deleteWarningTextTemplate: function deleteWarningTextTemplate(label) {
      return "You are about to delete view ".concat(label, ".");
    },
    deleteWarningCancel: 'Cancel',
    deleteWarningConfirm: 'Delete',
    editIconText: 'edit',
    listTitle: 'Available Views',
    modalTitle: 'Manage views',
    privateLabelText: 'Private',
    publicLabelText: 'Public',
    publicCheckboxLabelText: 'Display public views',
    searchPlaceholderText: 'Search views',
    searchClearButtonLabelText: 'Clear search input',
    searchIconLabelText: 'Search'
  },
  isLoading: false,
  overrides: undefined,
  searchValueDefault: '',
  testId: 'TableManageViewsModal',
  pagination: undefined
};
var defaultFunc = function defaultFunc() {};
var TableManageViewsModal = function TableManageViewsModal(_ref) {
  var _overrides$mainModal, _overrides$search, _overrides$publicChec, _overrides$tableManag, _overrides$warningMod, _overrides$mainModal2, _overrides$search2, _overrides$publicChec2, _overrides$tableManag2, _overrides$warningMod2;
  var _ref$actions = _ref.actions,
    _ref$actions$onEdit = _ref$actions.onEdit,
    onEdit = _ref$actions$onEdit === void 0 ? defaultFunc : _ref$actions$onEdit,
    _ref$actions$onDelete = _ref$actions.onDelete,
    onDelete = _ref$actions$onDelete === void 0 ? defaultFunc : _ref$actions$onDelete,
    _ref$actions$onClearE = _ref$actions.onClearError,
    onClearError = _ref$actions$onClearE === void 0 ? defaultFunc : _ref$actions$onClearE,
    onClose = _ref$actions.onClose,
    onSearchChange = _ref$actions.onSearchChange,
    onDisplayPublicChange = _ref$actions.onDisplayPublicChange,
    defaultViewId = _ref.defaultViewId,
    displayPublicDefaultChecked = _ref.displayPublicDefaultChecked,
    error = _ref.error,
    i18n = _ref.i18n,
    isLoading = _ref.isLoading,
    open = _ref.open,
    overrides = _ref.overrides,
    pagination = _ref.pagination,
    searchValueDefault = _ref.searchValueDefault,
    testID = _ref.testID,
    testId = _ref.testId,
    views = _ref.views;
  var _useMerged = useMerged(defaultProps.i18n, i18n),
    closeIconDescription = _useMerged.closeIconDescription,
    deleteWarningTextTemplate = _useMerged.deleteWarningTextTemplate,
    deleteWarningCancel = _useMerged.deleteWarningCancel,
    deleteWarningConfirm = _useMerged.deleteWarningConfirm,
    modalTitle = _useMerged.modalTitle,
    publicCheckboxLabelText = _useMerged.publicCheckboxLabelText,
    searchPlaceholderText = _useMerged.searchPlaceholderText,
    searchClearButtonLabelText = _useMerged.searchClearButtonLabelText,
    searchIconLabelText = _useMerged.searchIconLabelText,
    mergedI18n = _objectWithoutProperties(_useMerged, _excluded);
  var primaryInputId = 'manage-views-modal-search';
  var MyMainModal = (overrides === null || overrides === void 0 || (_overrides$mainModal = overrides.mainModal) === null || _overrides$mainModal === void 0 ? void 0 : _overrides$mainModal.component) || ComposedModal;
  var MySearch = (overrides === null || overrides === void 0 || (_overrides$search = overrides.search) === null || _overrides$search === void 0 ? void 0 : _overrides$search.component) || Search;
  var MyPublicCheckbox = (overrides === null || overrides === void 0 || (_overrides$publicChec = overrides.publicCheckbox) === null || _overrides$publicChec === void 0 ? void 0 : _overrides$publicChec.component) || Checkbox;
  var MyTableManageViewsList = (overrides === null || overrides === void 0 || (_overrides$tableManag = overrides.tableManageViewsList) === null || _overrides$tableManag === void 0 ? void 0 : _overrides$tableManag.component) || TableManageViewsList;
  var MyWarningModal = (overrides === null || overrides === void 0 || (_overrides$warningMod = overrides.warningModal) === null || _overrides$warningMod === void 0 ? void 0 : _overrides$warningMod.component) || Modal;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showDeleteWarning = _useState2[0],
    setShowDeleteWarning = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    viewIdToDelete = _useState4[0],
    setViewIdToDelete = _useState4[1];
  var getDeleteWarningText = function getDeleteWarningText() {
    var viewTitle = views.find(function (view) {
      return view.id === viewIdToDelete;
    }).title;
    return typeof deleteWarningTextTemplate === 'function' ? deleteWarningTextTemplate(viewTitle) : deleteWarningTextTemplate.replace('{0}', viewTitle);
  };
  var onShowWarning = function onShowWarning(id) {
    setViewIdToDelete(id);
    setShowDeleteWarning(true);
  };
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(MyMainModal, _extends({
    className: "".concat(iotPrefix, "--manage-views-modal")
    // TODO: remove deprecated 'testID' in v3.
    ,
    testID: testID || testId,
    error: error,
    header: {
      title: modalTitle
    },
    iconDescription: closeIconDescription,
    onClearError: onClearError,
    onClose: onClose,
    open: open,
    passiveModal: true,
    selectorPrimaryFocus: "#".concat(primaryInputId)
  }, overrides === null || overrides === void 0 || (_overrides$mainModal2 = overrides.mainModal) === null || _overrides$mainModal2 === void 0 ? void 0 : _overrides$mainModal2.props), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--manage-views-modal__filter-container")
  }, /*#__PURE__*/React__default.createElement(MySearch, _extends({
    className: "".concat(iotPrefix, "--manage-views-modal__search"),
    defaultValue: searchValueDefault,
    closeButtonLabelText: searchClearButtonLabelText,
    id: primaryInputId,
    name: "searchValue",
    labelText: searchIconLabelText,
    onChange: function onChange(evt) {
      var _evt$target;
      return onSearchChange(evt === null || evt === void 0 || (_evt$target = evt.target) === null || _evt$target === void 0 ? void 0 : _evt$target.value);
    },
    placeholder: searchPlaceholderText,
    size: "lg",
    type: "text"
  }, overrides === null || overrides === void 0 || (_overrides$search2 = overrides.search) === null || _overrides$search2 === void 0 ? void 0 : _overrides$search2.props)), /*#__PURE__*/React__default.createElement(MyPublicCheckbox, _extends({
    name: "isPublic"
    // TODO: remove deprecated 'testID' in v3.
    ,
    "data-testid": "".concat(testID || testId, "-public-checkbox"),
    defaultChecked: displayPublicDefaultChecked,
    id: "manage-views-modal-public-checkbox-label",
    labelText: publicCheckboxLabelText,
    wrapperClassName: "".concat(iotPrefix, "--manage-views-modal__public-checkbox"),
    onChange: function onChange(event, _ref2) {
      var isPublic = _ref2.checked;
      return onDisplayPublicChange(isPublic);
    }
  }, overrides === null || overrides === void 0 || (_overrides$publicChec2 = overrides.publicCheckbox) === null || _overrides$publicChec2 === void 0 ? void 0 : _overrides$publicChec2.props))), /*#__PURE__*/React__default.createElement(MyTableManageViewsList, _extends({
    defaultViewId: defaultViewId,
    i18n: mergedI18n,
    isLoading: isLoading,
    onEdit: onEdit,
    onDelete: onShowWarning,
    pagination: pagination
    // TODO: remove deprecated 'testID' in v3.
    ,
    testID: "".concat(testID || testId, "-views-list"),
    views: views
  }, overrides === null || overrides === void 0 || (_overrides$tableManag2 = overrides.tableManageViewsList) === null || _overrides$tableManag2 === void 0 ? void 0 : _overrides$tableManag2.props))), showDeleteWarning && /*#__PURE__*/React__default.createElement(MyWarningModal, _extends({
    className: "".concat(iotPrefix, "--manage-views-modal-warning"),
    closeButtonLabel: closeIconDescription,
    modalHeading: getDeleteWarningText(),
    onRequestClose: function onRequestClose() {
      setShowDeleteWarning(false);
    },
    onRequestSubmit: function onRequestSubmit() {
      onDelete(viewIdToDelete);
      setShowDeleteWarning(false);
    },
    open: showDeleteWarning,
    primaryButtonText: deleteWarningConfirm,
    secondaryButtonText: deleteWarningCancel,
    size: "sm"
  }, overrides === null || overrides === void 0 || (_overrides$warningMod2 = overrides.warningModal) === null || _overrides$warningMod2 === void 0 ? void 0 : _overrides$warningMod2.props)));
};
TableManageViewsModal.propTypes = propTypes;
TableManageViewsModal.defaultProps = defaultProps;
TableManageViewsModal.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TableManageViewsModal",
  "props": {
    "defaultViewId": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "The ID of the view that should be marked as default if any",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "displayPublicDefaultChecked": {
      "defaultValue": {
        "value": "true",
        "computed": false
      },
      "description": "True means that the 'display public' checkbox is checked by default",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "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  closeIconDescription: 'Close',\n  defaultLabelText: 'default',\n  deleteIconText: 'delete',\n  deleteWarningTextTemplate: (label) => `You are about to delete view ${label}.`,\n  deleteWarningCancel: 'Cancel',\n  deleteWarningConfirm: 'Delete',\n  editIconText: 'edit',\n  listTitle: 'Available Views',\n  modalTitle: 'Manage views',\n  privateLabelText: 'Private',\n  publicLabelText: 'Public',\n  publicCheckboxLabelText: 'Display public views',\n  searchPlaceholderText: 'Search views',\n  searchClearButtonLabelText: 'Clear search input',\n  searchIconLabelText: 'Search',\n}",
        "computed": false
      },
      "description": "Internationalization strings object",
      "type": {
        "name": "shape",
        "value": {
          "closeIconDescription": {
            "name": "string",
            "required": false
          },
          "defaultLabelText": {
            "name": "string",
            "required": false
          },
          "deleteIconText": {
            "name": "string",
            "required": false
          },
          "deleteWarningTextTemplate": {
            "name": "union",
            "value": [{
              "name": "func"
            }, {
              "name": "string"
            }],
            "required": false
          },
          "deleteWarningCancel": {
            "name": "string",
            "required": false
          },
          "deleteWarningConfirm": {
            "name": "string",
            "required": false
          },
          "editIconText": {
            "name": "string",
            "required": false
          },
          "listTitle": {
            "name": "string",
            "required": false
          },
          "modalTitle": {
            "name": "string",
            "required": false
          },
          "privateLabelText": {
            "name": "string",
            "required": false
          },
          "publicLabelText": {
            "name": "string",
            "required": false
          },
          "publicCheckboxLabelText": {
            "name": "string",
            "required": false
          },
          "searchPlaceholderText": {
            "name": "string",
            "required": false
          },
          "searchClearButtonLabelText": {
            "name": "string",
            "required": false
          },
          "searchIconLabelText": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "isLoading": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Shows loading skeleton and disables pagination when true",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "overrides": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Used to override the internal components and props for advanced customization",
      "type": {
        "name": "shape",
        "value": {
          "mainModal": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "publicCheckbox": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "search": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "tableManageViewsList": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "warningModal": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          }
        }
      },
      "required": false
    },
    "searchValueDefault": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "The default value of the search field value",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'TableManageViewsModal'",
        "computed": false
      },
      "description": "Id that can be used for testing",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "pagination": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "pagination at the bottom of list, see SimplePaginationPropTypes",
      "type": {
        "name": "shape",
        "value": "import { SimplePaginationPropTypes } from '../../SimplePagination/SimplePagination';",
        "computed": true
      },
      "required": false
    },
    "actions": {
      "description": "Callbacks for the actions of the modal\nonEdit : Called when the user clicks edit on a view row. The ID of the view is passed as argument.\nonDelete : Called when the user clicks delete on a view row. The ID of the view is passed as argument.\nonClearError : Called when the dialog error msg is cleared\nonSearchChange : Called when the search input value is changed, the search term is passed as argument.\nonDisplayPublicChange : Called when view title input value is changed, a bool value is passed as argument.",
      "type": {
        "name": "shape",
        "value": {
          "onClose": {
            "name": "func",
            "required": false
          },
          "onEdit": {
            "name": "func",
            "required": false
          },
          "onDelete": {
            "name": "func",
            "required": false
          },
          "onClearError": {
            "name": "func",
            "required": false
          },
          "onSearchChange": {
            "name": "func",
            "required": true
          },
          "onDisplayPublicChange": {
            "name": "func",
            "required": true
          }
        }
      },
      "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
    },
    "views": {
      "description": "The views to be displayed in the list, see ViewsPropType",
      "type": {
        "name": "custom",
        "raw": "ViewsPropType.isRequired"
      },
      "required": false
    }
  }
};
var TableManageViewsModal$1 = TableManageViewsModal;

export { TableManageViewsModal$1 as default };
