'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _icon = require('../../icon');

var _icon2 = _interopRequireDefault(_icon);

var _col = require('../../col');

var _col2 = _interopRequireDefault(_col);

var _context = require('./context');

var _useLocalReceiver = require('./useLocalReceiver');

var _default = require('../../locale/default');

var _default2 = _interopRequireDefault(_default);

var _tooltip = require('../../tooltip');

var _tooltip2 = _interopRequireDefault(_tooltip);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

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

var __rest = undefined && undefined.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};

function toTooltipProps(tooltip) {
    if (!tooltip) {
        return null;
    }
    if (typeof tooltip === 'object' && !React.isValidElement(tooltip)) {
        return tooltip;
    }
    return {
        title: tooltip
    };
}
var FormItemLabel = function FormItemLabel(_ref) {
    var prefixCls = _ref.prefixCls,
        label = _ref.label,
        htmlFor = _ref.htmlFor,
        labelCol = _ref.labelCol,
        labelAlign = _ref.labelAlign,
        colon = _ref.colon,
        required = _ref.required,
        requiredMark = _ref.requiredMark,
        tooltip = _ref.tooltip;

    var _useLocaleReceiver = (0, _useLocalReceiver.useLocaleReceiver)('Form'),
        _useLocaleReceiver2 = (0, _slicedToArray3['default'])(_useLocaleReceiver, 1),
        formLocale = _useLocaleReceiver2[0];

    if (!label) return null;
    return React.createElement(
        _context.FormContext.Consumer,
        { key: 'label' },
        function (_ref2) {
            var _classNames;

            var vertical = _ref2.vertical,
                contextLabelAlign = _ref2.labelAlign,
                contextLabelCol = _ref2.labelCol,
                contextColon = _ref2.colon;

            var _a;
            var mergedLabelCol = labelCol || contextLabelCol || {};
            var mergedLabelAlign = labelAlign || contextLabelAlign;
            var labelClsBasic = prefixCls + '-item-label';
            var labelColClassName = (0, _classnames2['default'])(labelClsBasic, mergedLabelAlign === 'left' && labelClsBasic + '-left', mergedLabelCol.className);
            var labelChildren = label;
            // Keep label is original where there should have no colon
            var computedColon = colon === true || contextColon !== false && colon !== false;
            var haveColon = computedColon && !vertical;
            // Remove duplicated user input colon
            if (haveColon && typeof label === 'string' && label.trim() !== '') {
                labelChildren = label.replace(/[:|：]\s*$/, '');
            }
            // Tooltip
            var tooltipProps = toTooltipProps(tooltip);
            if (tooltipProps) {
                var _tooltipProps$icon = tooltipProps.icon,
                    icon = _tooltipProps$icon === undefined ? React.createElement(_icon2['default'], { type: 'question-circle', theme: 'filled' }) : _tooltipProps$icon,
                    restTooltipProps = __rest(tooltipProps, ["icon"]);

                var tooltipNode = React.createElement(
                    _tooltip2['default'],
                    restTooltipProps,
                    React.cloneElement(icon, { className: prefixCls + '-item-tooltip' })
                );
                labelChildren = React.createElement(
                    React.Fragment,
                    null,
                    labelChildren,
                    tooltipNode
                );
            }
            // Add required mark if optional
            if (requiredMark === 'optional' && !required) {
                labelChildren = React.createElement(
                    React.Fragment,
                    null,
                    labelChildren,
                    React.createElement(
                        'span',
                        { className: prefixCls + '-item-optional' },
                        (formLocale === null || formLocale === void 0 ? void 0 : formLocale.optional) || ((_a = _default2['default'].Form) === null || _a === void 0 ? void 0 : _a.optional)
                    )
                );
            }
            var labelClassName = (0, _classnames2['default'])((_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-item-required', required), (0, _defineProperty3['default'])(_classNames, prefixCls + '-item-required-mark-optional', requiredMark === 'optional'), (0, _defineProperty3['default'])(_classNames, prefixCls + '-item-no-colon', !computedColon), _classNames));
            return React.createElement(
                _col2['default'],
                (0, _extends3['default'])({}, mergedLabelCol, { className: labelColClassName }),
                React.createElement(
                    'label',
                    { htmlFor: htmlFor, className: labelClassName, title: typeof label === 'string' ? label : '' },
                    labelChildren
                )
            );
        }
    );
};
exports['default'] = FormItemLabel;
module.exports = exports['default'];