"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = InputTable;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactIntl = require("react-intl");

var _get = _interopRequireDefault(require("lodash/get"));

var _cspaceInput = require("cspace-input");

var _configHelpers = require("../../helpers/configHelpers");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const {
  Label,
  InputTable: BaseInputTable
} = _cspaceInput.components;
const {
  pathHelpers
} = _cspaceInput.helpers;

const renderMessageLabel = (message, props) => /*#__PURE__*/_react.default.createElement(Label, props, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, message));

const renderTableLabel = (recordTypeConfig, name) => {
  const message = (0, _get.default)(recordTypeConfig, ['messages', 'inputTable', name]);
  return message ? renderMessageLabel(message) : null;
};

const renderFieldLabel = (fieldConfig, inputProps) => {
  const message = (0, _get.default)(fieldConfig, ['messages', 'name']);
  const props = {
    readOnly: inputProps.readOnly,
    required: inputProps.required
  };

  if ('readOnly' in fieldConfig) {
    props.readOnly = fieldConfig.readOnly;
  }

  if ('required' in fieldConfig) {
    props.required = fieldConfig.required;
  }

  return message ? renderMessageLabel(message, props) : null;
};

const propTypes = {
  children: _propTypes.default.node,
  name: _propTypes.default.string
};
const contextTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  intl: _reactIntl.intlShape,
  recordType: _propTypes.default.string
};

function InputTable(props, context) {
  const {
    children,
    name
  } = props;
  const {
    config,
    intl,
    recordType
  } = context;
  const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
  const fields = (0, _get.default)(recordTypeConfig, 'fields');

  const renderLabel = input => {
    const path = (0, _configHelpers.dataPathToFieldDescriptorPath)(pathHelpers.getPath(input.props));
    const field = (0, _get.default)(fields, path);
    const fieldConfig = (0, _get.default)(field, _configHelpers.configKey);
    return fieldConfig && renderFieldLabel(fieldConfig, input.props);
  };

  const renderAriaLabel = input => {
    const path = (0, _configHelpers.dataPathToFieldDescriptorPath)(pathHelpers.getPath(input.props));
    const field = (0, _get.default)(fields, path);
    const messages = (0, _get.default)(field, [_configHelpers.configKey, 'messages']);
    const message = messages && (messages.fullName || messages.name);
    return message && intl.formatMessage(message);
  };

  const tableLabel = renderTableLabel(recordTypeConfig, name);
  return /*#__PURE__*/_react.default.createElement(BaseInputTable, {
    label: tableLabel,
    renderLabel: renderLabel,
    renderAriaLabel: renderAriaLabel
  }, children);
}

InputTable.propTypes = propTypes;
InputTable.contextTypes = contextTypes;