'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports['default'] = useItemRef;

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _ref = require('rc-util/lib/ref');

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

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 useItemRef() {
    var _React$useContext = React.useContext(_context.FormContext),
        itemRef = _React$useContext.itemRef;

    var cacheRef = React.useRef({});
    function getRef(name, children) {
        var childrenRef = children && typeof children === 'object' && children.ref;
        var nameStr = name.join('_');
        if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
            cacheRef.current.name = nameStr;
            cacheRef.current.originRef = childrenRef;
            cacheRef.current.ref = (0, _ref.composeRef)(itemRef(name), childrenRef);
        }
        return cacheRef.current.ref;
    }
    return getRef;
}
module.exports = exports['default'];