export default {};
export const queryToUrl = query => {
  return Object.keys(query).reduce((sum, item) => `${sum}&${item}=${query[item]}`, '');
}; // 页面跳转

export const history = (() => {
  const storage = ({
    pageType,
    componentData
  }) => {
    if (!getApp().globalData.pageTemp) {
      getApp().globalData.pageTemp = {};
    }

    getApp().globalData.pageTemp[pageType] = componentData;
  };

  const url = ({
    pageType,
    query,
    title,
    pull
  }) => `/antbuilder/industry/soldier/pages/${pull ? 'page-pull' : 'page-no-pull'}/index?title=${title}&pageType=${pageType}${queryToUrl(query)}`; // pageType 指定渲染的组件
  // data 数据，用于复杂类型的传递
  // query 可以通过分享出去的链接重新回到该页面，简单的query数据
  // title 页面标题和分享出去的标题
  // pull 页面是否允许下拉


  return {
    push({
      pageType,
      componentData,
      query = {},
      title,
      pull = true
    }) {
      storage({
        pageType,
        componentData
      });
      my.navigateTo({
        url: url({
          pageType,
          query,
          title,
          pull
        })
      });
    },

    replace({
      pageType,
      componentData,
      query = {},
      title,
      pull
    }) {
      storage({
        pageType,
        componentData
      });
      my.redirectTo({
        url: url({
          pageType,
          query,
          title,
          pull
        })
      });
    }

  };
})();