import { _ as _extends } from '../_rollupPluginBabelHelpers-736b301a.js';
import React__default, { useEffect } from 'react';
import { Switch, Route } from 'react-router-dom';
import { isEqual, cache, logger } from '@zenview/micro-utils';

const getRouteItem = (match, routes) => {
  const matchParams = match.params || {};
  let routeItem = routes.find(v => v.path === match.url);
  if (routeItem) {
    return routeItem;
  } else {
    const keys = Object.keys(matchParams);
    const tempPaths = routes.map(v => v.path);
    tempPaths.forEach((path, index) => {
      let newPath = path;
      keys.forEach(key => {
        if (path.includes(`:${key}`)) {
          newPath = path.replace(`:${key}`, matchParams[key]);
        }
      });
      tempPaths[index] = newPath;
    });
    const index = tempPaths.findIndex(v => v === match.url);
    return routes[index];
  }
};

/**
 * @TODO
 * @多种修正使用url判断异步处理这里防止回调执行的时候url变化导致一直执行
 * @每次执行先清除上一次的
 */

const RenderRoute = /*#__PURE__*/React__default.memo(({
  location,
  match,
  history,
  setRouteProps
}) => {
  // eslint-disable-next-line react-hooks/exhaustive-deps
  useEffect(() => setRouteProps(match, location, history), [location.pathname]);
  return null;
});
const RenderComponents = /*#__PURE__*/React__default.memo(({
  state,
  props
}) => {
  const {
    match,
    history,
    location
  } = state;
  const {
    path,
    component,
    render,
    routes,
    ...other
  } = props;
  let Com, Ren;
  if (!routes) {
    Com = component;
    Ren = render;
  } else {
    const routeItem = getRouteItem(match, routes);
    Com = routeItem.component;
    Ren = routeItem.render;
  }
  return Com ? /*#__PURE__*/React__default.cloneElement(/*#__PURE__*/React__default.createElement(Com, null), {
    ...other,
    match,
    location,
    history
  }) : Ren({
    ...other,
    match,
    location,
    history
  });
});

class AppRoute extends React__default.Component {
  constructor() {
    super();
    //可能存在路由创建但是没有render 导致UI无法渲染
    this.setRouteProps = (newMatch, newLocation, history) => {
      if (!isEqual(this.props.match, newMatch)) {
        this.setState({
          match: newMatch,
          location: newLocation,
          history
        });
      }
    };
    this.renderRoute = params => {
      return /*#__PURE__*/React__default.createElement(RenderRoute, _extends({}, params, {
        setRouteProps: this.setRouteProps
      }));
    };
    this.renderComponent = () => {
      this.isRender = true;
      return /*#__PURE__*/React__default.createElement(RenderComponents, {
        state: this.state,
        props: this.props
      });
    };
    this.isRender = false;
    this.state = {
      match: undefined,
      location: undefined,
      history: undefined
    };
  }
  shouldComponentUpdate(props, state) {
    var _state$location;
    if (!window._IS_RUN_MICRO_BASIC || !this.isRender) {
      return true;
    }
    const currentId = cache.getCache('currentTabKey', 'session');
    return currentId === props.tabId && props.location.pathname === ((_state$location = state.location) === null || _state$location === void 0 ? void 0 : _state$location.pathname);
  }
  componentWillUnmount() {
    logger.debug('路由销毁');
  }
  render() {
    const {
      match,
      location
    } = this.state;
    const {
      path,
      routes,
      exact,
      sensitive,
      strict,
      children
    } = this.props;
    const isRouteMath = match && location;
    return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Switch, null, routes ? routes.map(item => /*#__PURE__*/React__default.createElement(Route, {
      exact: !!item.exact,
      sensitive: !!item.sensitive,
      strict: !!item.strict,
      key: item.path,
      path: item.path,
      component: this.renderRoute
    })) : /*#__PURE__*/React__default.createElement(Route, {
      exact: !!exact,
      strict: !!strict,
      sensitive: !!sensitive,
      path: path,
      component: this.renderRoute
    }), children), isRouteMath && this.renderComponent());
  }
}

export { AppRoute as default };
//# sourceMappingURL=index.js.map
