import _extends from "@babel/runtime/helpers/extends";
import React, { isValidElement, useCallback, useContext } from 'react';
import { observer } from 'mobx-react-lite';
import ObserverTextField from '../../../text-field/TextField';
import TableContext from '../../TableContext';
import { $l } from '../../../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 $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 = useContext(TableContext),
      tableStore = _useContext.tableStore;

  var columnTitleEditable = tableStore.columnTitleEditable;
  var editing = record.getState('editing');
  var handleEditBlur = useCallback(function () {
    record.setState('editing', false);
  }, []);
  var handleHeaderChange = 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.createElement(React.Fragment, null, editing && titleEditable ? /*#__PURE__*/React.createElement(ObserverTextField, {
    onBlur: handleEditBlur,
    value: /*#__PURE__*/isValidElement(header) ? title : header,
    onChange: handleHeaderChange,
    autoFocus: true
  }) : /*#__PURE__*/React.createElement("span", _extends({}, provided && provided.dragHandleProps, {
    style: provided ? {
      cursor: 'move'
    } : undefined
  }), header));
};

ItemTitle.displayName = 'ItemTitle';
export default observer(ItemTitle);
//# sourceMappingURL=ItemTitle.js.map
