import {
  createHashHistory,
  createBrowserHistory,
  createMemoryHistory,
  HashHistoryBuildOptions,
  BrowserHistoryBuildOptions,
  MemoryHistoryBuildOptions,
} from 'history';
import { runtime } from './adapter';

export type HistoryOptions =
  | HashHistoryBuildOptions
  | BrowserHistoryBuildOptions;

// 显式声明HASH 或 app 的 路由使用 hash router，而其他web应用使用 BrowserRouter
const createHistory = (options: HistoryOptions, type = 'browser') => {
  let history;
  switch (type) {
    case 'hash':
      history = createHashHistory({ basename: '', ...options });
      break;
    case 'memory':
      history = createMemoryHistory();
      break;
    default:
      history = createBrowserHistory(options);
  }
  (runtime as any)._WEAPPS_HISTORY = history;
  return history;
};

const removeHistory = () => {
  (runtime as any)._WEAPPS_HISTORY = undefined;
};

export { createHistory, removeHistory };
