'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.getKeys = getKeys;
exports.getAbsolutePath = getAbsolutePath;
exports.getHeaders = getHeaders;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _toflat = require('../toflat');

var _toflat2 = _interopRequireDefault(_toflat);

var _JSONLike = require('../JSONLike');

var _JSONLike2 = _interopRequireDefault(_JSONLike);

var _Table = require('./Table.scss');

var _Table2 = _interopRequireDefault(_Table);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function getKeys(data, isFlat) {
	if (isFlat) {
		return Object.keys((0, _toflat2.default)(data));
	}
	return Object.keys(data);
}

function getAbsolutePath(index, key, flat) {
	if (flat) {
		return '$[' + index + ']' + key.replace('$', '');
	}
	return '$[' + index + '][\'' + key + '\']';
}

function getHeaders(keys, isFlat) {
	if (isFlat) {
		// $['id'][0]['foo'] -> id[0].foo
		return keys.map(function (str) {
			return str.replace('$[\'', '').replace('\'][\'', '.').replace('][\'', '].').replace('\'][', '[').replace('\']', '');
		});
	}
	return keys;
}

function Table(_ref) {
	var flat = _ref.flat,
	    data = _ref.data,
	    props = _objectWithoutProperties(_ref, ['flat', 'data']);

	if (!Array.isArray(data)) {
		return null;
	}
	var keys = getKeys(data[0], flat);
	var headers = getHeaders(keys, flat);
	var tableClassName = (0, _classnames2.default)(_Table2.default.table, 'tc-object-viewer', 'table table-bordered table-striped table-hover');

	return _react2.default.createElement(
		'table',
		{ className: tableClassName },
		_react2.default.createElement(
			'thead',
			null,
			_react2.default.createElement(
				'tr',
				null,
				headers.map(function (key, index) {
					return _react2.default.createElement(
						'td',
						{ key: index },
						key
					);
				})
			)
		),
		_react2.default.createElement(
			'tbody',
			null,
			data.map(function (row, index) {
				var flattenRow = flat ? (0, _toflat2.default)(row) : row;
				return _react2.default.createElement(
					'tr',
					{ key: index },
					keys.map(function (key, j) {
						var path = getAbsolutePath(index, key, flat);
						return _react2.default.createElement(
							'td',
							{ key: j },
							_react2.default.createElement(_JSONLike2.default, _extends({
								data: flattenRow[key]
							}, props, {
								jsonpath: path
							}))
						);
					})
				);
			})
		)
	);
}

Table.propTypes = {
	flat: _react.PropTypes.bool,
	data: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.number, _react.PropTypes.string, _react.PropTypes.object, _react.PropTypes.array])
};

exports.default = Table;