import { prefixPagePath, industryEntry } from "../../../core/utils";
import { queryToUrl } from './index';
import masConfig from '../mas.config.json';
export const getEntryPage = pull => `${industryEntry}/${masConfig['industry-name']}/pages/${pull ? 'page-pull' : 'page-no-pull'}/index`;

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

    getApp().globalData.pageTemp[pageType] = componentData;
  }; // eslint-disable-next-line max-len
  // /components/industry-component/volunteer/pages/page-no-pull/index?title=标题&pageType=volunteer-projects


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


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

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

    toH5(_url) {
      my.navigateTo({
        url: `${prefixPagePath}/h5/index?url=${_url}`
      });
    }

  };
})();

export default history;