'use strict';

exports.__esModule = true;
exports.useWithFallback = exports.swatch = exports.srgbItem = undefined;

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactSeeds = require('react-seeds');

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

var hexColorRegex = /^[a-fA-F0-9]{3,6}$/;
var defaultSwatchSize = 32;

var parseColorValue = function parseColorValue() {
	var alpha = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
	return _ramda2.default.ifElse(_ramda2.default.test(hexColorRegex), function (value) {
		return '#' + value;
	}, _ramda2.default.pipe(_ramda2.default.split(/[,]?\s+/), _ramda2.default.cond([[_ramda2.default.propEq('length', 1), _ramda2.default.pipe(_ramda2.default.head, _ramda2.default.repeat(_ramda2.default.__, 3), _ramda2.default.append(alpha))], [_ramda2.default.propEq('length', 3), _ramda2.default.append(alpha)], [_ramda2.default.T, _ramda2.default.identity]]), _ramda2.default.join(','), function (value) {
		return 'rgba(' + value + ')';
	}));
};

var parseAlpha = _ramda2.default.pipe(parseFloat, _ramda2.default.when(isNaN, function () {}));

var srgbItem = exports.srgbItem = function srgbItem(value, alpha, minWidth, minHeight) {
	return _react2.default.createElement(_reactSeeds.Seed, {
		minWidth: minWidth,
		minHeight: minHeight,
		background: {
			color: parseColorValue(alpha)(value)
		}
	});
};

var swatch = exports.swatch = function swatch(tags, references, text, children, Element, resolveContent, _ref) {
	var elementFromText = _ref.elementFromText;

	var alpha = parseAlpha(resolveContent(tags.alpha));
	var minWidth = _ramda2.default.cond([[_ramda2.default.has('width'), _ramda2.default.pipe(_ramda2.default.prop('width'), resolveContent)], [_ramda2.default.has('size'), _ramda2.default.pipe(_ramda2.default.prop('size'), resolveContent)], [_ramda2.default.T, _ramda2.default.always(defaultSwatchSize)]])(tags);
	var minHeight = _ramda2.default.cond([[_ramda2.default.has('height'), _ramda2.default.pipe(_ramda2.default.prop('height'), resolveContent)], [_ramda2.default.has('size'), _ramda2.default.pipe(_ramda2.default.prop('size'), resolveContent)], [_ramda2.default.T, _ramda2.default.always(defaultSwatchSize)]])(tags);

	var content = resolveContent({ references: references, text: text });
	console.log('#swatch content', content);
	if (Array.isArray(content)) {
		children = content.map(elementFromText);
	}

	if (children.length > 0) {
		return _react2.default.createElement(
			_reactSeeds.Seed,
			{ row: true, wrap: true, alignItems: 'center' },
			Element.renderArray({ defaultTags: tags }, children)
		);
	} else if (_ramda2.default.is(String, content)) {
		return srgbItem(content, alpha, minWidth, minHeight);
	} else {
		return _react2.default.createElement('noscript', null);
	}
};

var useWithFallback = exports.useWithFallback = function useWithFallback(fallback) {
	return _ramda2.default.cond([[_ramda2.default.has('swatch'), _ramda2.default.curry(swatch)], [_ramda2.default.T, fallback]]);
};