'use strict';

require('core-js/modules/es.regexp.exec.js');
require('core-js/modules/es.string.search.js');
var React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var Settings = require('../../../constants/Settings.js');
var componentUtilityFunctions = require('../../../utils/componentUtilityFunctions.js');

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

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

var iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  title: PropTypes__default.default.string,
  buttons: PropTypes__default.default.arrayOf(PropTypes__default.default.node),
  search: PropTypes__default.default.shape({
    onChange: PropTypes__default.default.func,
    value: PropTypes__default.default.string,
    id: PropTypes__default.default.string,
    hasFastSearch: PropTypes__default.default.bool
  }),
  i18n: PropTypes__default.default.shape({
    searchPlaceHolderText: PropTypes__default.default.string,
    clearSearchIconDescription: PropTypes__default.default.string
  }),
  testId: PropTypes__default.default.string
};
var defaultProps = {
  buttons: [],
  search: {
    onChange: function onChange() {},
    value: '',
    hasFastSearch: true
  },
  i18n: {
    searchPlaceHolderText: 'Enter a value'
  },
  title: null,
  testId: 'list-header'
};
var ListHeader = function ListHeader(_ref) {
  var title = _ref.title,
    buttons = _ref.buttons,
    search = _ref.search,
    i18n = _ref.i18n,
    testId = _ref.testId;
  var previousSearchValueRef = React.useRef(search === null || search === void 0 ? void 0 : search.value);
  var hasFastSearch = (search === null || search === void 0 ? void 0 : search.hasFastSearch) !== false;
  var handleSearch = function handleSearch(e) {
    var currentValue = e.target.value;
    var previousValue = previousSearchValueRef.current;
    if (previousValue !== currentValue) {
      search.onChange(e);
      previousSearchValueRef.current = currentValue;
    }
  };
  return /*#__PURE__*/React__default.default.createElement("div", {
    "data-testid": testId,
    className: "".concat(iotPrefix, "--list-header-container")
  }, title || buttons && buttons.length > 0 ? /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--list-header")
  }, /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--list-header--title")
  }, title), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--list-header--btn-container")
  }, buttons)) : null, search ? /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--list-header--search")
  }, /*#__PURE__*/React__default.default.createElement(react.Search, {
    closeButtonLabelText: i18n.clearSearchIconDescription,
    id: search.id || "".concat(iotPrefix, "--list-header--search"),
    placeholder: i18n.searchPlaceHolderText,
    onChange: hasFastSearch !== false ? handleSearch : undefined,
    onBlur: hasFastSearch ? undefined : handleSearch,
    onKeyDown: componentUtilityFunctions.handleSpecificKeyDown(['Enter'], handleSearch),
    onClear: function onClear() {
      return handleSearch({
        target: {
          value: ''
        }
      });
    },
    size: "md",
    value: search.value,
    labelText: i18n.searchPlaceHolderText,
    "data-testid": "".concat(testId, "-search-input")
  })) : null);
};
ListHeader.propTypes = propTypes;
ListHeader.defaultProps = defaultProps;
ListHeader.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ListHeader",
  "props": {
    "buttons": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "node"
        }
      },
      "required": false
    },
    "search": {
      "defaultValue": {
        "value": "{\n  onChange: () => {},\n  value: '',\n  hasFastSearch: true,\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "onChange": {
            "name": "func",
            "required": false
          },
          "value": {
            "name": "string",
            "required": false
          },
          "id": {
            "name": "string",
            "required": false
          },
          "hasFastSearch": {
            "name": "bool",
            "required": false
          }
        }
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  searchPlaceHolderText: 'Enter a value',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "searchPlaceHolderText": {
            "name": "string",
            "required": false
          },
          "clearSearchIconDescription": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "title": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'list-header'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    }
  }
};
var DefaultListHeader = ListHeader;

module.exports = DefaultListHeader;
