import {Modal} from 'antd';
import {
  RouteMatch,
  Router as BoringRouter,
  RouterType,
  schema,
} from 'boring-router';
import {createBrowserHistory} from 'history';

export const routeSchema = schema({
  home: true,
  commands: {
    $exact: true,
    $children: {
      history: {
        $exact: true,
        $children: {
          commandId: {
            $exact: true,
            $match: /\d+/,
          },
        },
        $query: {
          'only-unexecuted': true,
        },
      },
      management: {
        $exact: true,
        $children: {
          commandName: {
            $exact: true,
            $match: RouteMatch.segment,
          },
        },
      },
    },
  },
  makeflow: {
    $exact: true,
    $children: {
      login: true,
    },
  },
  configs: true,
  tokens: true,
  login: true,
  initialize: true,
  $rest: {
    $match: /.*/,
  },
});

export const history = createBrowserHistory();

export const router = BoringRouter.create(routeSchema, history, {
  onChange() {
    Modal.destroyAll();
  },
});

export type Router = RouterType<typeof routeSchema, object>;
