import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';

var _jsxFileName = 'src/breadcrumb/Breadcrumb.jsx',
    _this = this;

/**
 * Breadcrumb.jsx
 *
 * @Author: zhangce
 * @Date: 18/1/23 下午2:42
 */

import React, { Children, cloneElement, isValidElement, Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import BreadcrumbItem from './BreadcrumbItem';

var getBreadcrumbName = function getBreadcrumbName(route, params) {
    if (route.crumbName === void 0) {
        return null;
    }
    var paramsKeys = Object.keys(params).join('|');
    var name = route.crumbName.replace(new RegExp(':(' + paramsKeys + ')', 'g'), function (replacement, key) {
        return params[key] || replacement;
    });
    return name;
};
var defaultCrumbRender = function defaultCrumbRender(route, params, routes, paths) {
    var isLast = routes.indexOf(route) === routes.length - 1;
    var name = getBreadcrumbName(route, params);
    return isLast ? name : React.createElement(
        'a',
        { href: '#/' + paths.join('/'), __source: {
                fileName: _jsxFileName,
                lineNumber: 27
            },
            __self: _this
        },
        name
    );
};

/**
 * Breadcrumb
 * */

var Breadcrumb = function (_React$PureComponent) {
    _inherits(Breadcrumb, _React$PureComponent);

    function Breadcrumb() {
        _classCallCheck(this, Breadcrumb);

        return _possibleConstructorReturn(this, (Breadcrumb.__proto__ || Object.getPrototypeOf(Breadcrumb)).apply(this, arguments));
    }

    _createClass(Breadcrumb, [{
        key: 'render',
        value: function render() {
            var _this3 = this;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                className = _props.className,
                children = _props.children,
                separator = _props.separator,
                routes = _props.routes,
                params = _props.params,
                _props$crumbRender = _props.crumbRender,
                crumbRender = _props$crumbRender === undefined ? defaultCrumbRender : _props$crumbRender,
                other = _objectWithoutProperties(_props, ['prefixCls', 'className', 'children', 'separator', 'routes', 'params', 'crumbRender']);

            var classStr = classNames(prefixCls, className);
            var crumbs = void 0;

            if (routes && routes.length) {
                var paths = [];
                crumbs = routes.map(function (route, index) {
                    var path = route.path || '';
                    Object.keys(params).forEach(function (key) {
                        debugger;
                        path = path.replace(':' + key, params[key]);
                    });
                    if (path) {
                        paths.push(path);
                    }
                    return React.createElement(
                        BreadcrumbItem,
                        { separator: separator, key: index, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 89
                            },
                            __self: _this3
                        },
                        crumbRender(route, params, routes, paths)
                    );
                });
            } else {
                crumbs = Children.map(children, function (ele, index) {
                    if (!isValidElement(ele)) {
                        return ele;
                    }
                    return cloneElement(ele, {
                        key: index,
                        separator: separator
                    });
                });
            }
            return React.createElement(
                'div',
                Object.assign({ className: classStr }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 106
                    },
                    __self: this
                }),
                crumbs
            );
        }
    }]);

    return Breadcrumb;
}(React.PureComponent);

Breadcrumb.propTypes = {
    /**
     * 类前缀
     * */
    prefixCls: PropTypes.string,
    /**
     * 分隔符
     * */
    separator: PropTypes.node,
    /**
     * 路由参数
     * */
    params: PropTypes.object,
    /**
     * 路由栈
     * */
    routes: PropTypes.arrayOf(PropTypes.object),
    /**
     * 面包屑渲染函数
     * */
    crumbRender: PropTypes.func
};
Breadcrumb.defaultProps = {
    prefixCls: 'ne-breadcrumb',
    separator: '/',
    params: {}
};


export default Breadcrumb;