"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _mobxReactLite = require("mobx-react-lite");

var _TextField = _interopRequireDefault(require("../../../text-field/TextField"));

var _TableContext = _interopRequireDefault(require("../../TableContext"));

var _localeContext = require("../../../locale-context");

function getHeader(record, title) {
  if (title !== undefined) {
    if (typeof title === 'function') {
      var dom = title();

      if (dom && dom.type && dom.type.__PRO_CHECKBOX) {
        record.set('titleEditable', false);
        return (0, _localeContext.$l)('Table', 'select_current_page');
      }

      return dom;
    }

    return title;
  }

  return record.get('children')[0].props.children;
}

var ItemTitle = function ItemTitle(props) {
  var record = props.record,
      provided = props.provided;

  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      tableStore = _useContext.tableStore;

  var columnTitleEditable = tableStore.columnTitleEditable;
  var editing = record.getState('editing');
  var handleEditBlur = (0, _react.useCallback)(function () {
    record.setState('editing', false);
  }, []);
  var handleHeaderChange = (0, _react.useCallback)(function (value) {
    record.set('title', value);
  }, [record]);
  var title = record.get('title') || record.get('header');
  var header = getHeader(record, title);
  var titleEditable = columnTitleEditable && record.get('titleEditable') !== false;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, editing && titleEditable ? /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
    onBlur: handleEditBlur,
    value: /*#__PURE__*/(0, _react.isValidElement)(header) ? title : header,
    onChange: handleHeaderChange,
    autoFocus: true
  }) : /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, provided && provided.dragHandleProps, {
    style: provided ? {
      cursor: 'move'
    } : undefined
  }), header));
};

ItemTitle.displayName = 'ItemTitle';

var _default = (0, _mobxReactLite.observer)(ItemTitle);

exports["default"] = _default;
//# sourceMappingURL=ItemTitle.js.map
