/**
 * TEAM: frontend_infra
 * @flow strict
 * @deprecated prefer page props in Workspaces (or WorkspaceContext), next/router in Next.js
 */

import * as React from "react";
import {type BasicAnchorProps} from "../Link";

/** WARNING: Usage of RouterContext in Workspace apps may have unexpected
 * results if reading the URL, this is because when visiting a new page in
 * Workspace apps, the old page is kept in view while the next page is loading.
 * Your page may get an unexpected URL. As an alternative approach, you can
 * access the router method, match, and page location as page props in Workspace
 * apps, this does not apply in Next.js */

// TODO(ctan): Obsolete this context as part of Next.js migration
type Location = {|
  +hash: string,
  +pathname: string,
  +query: {[key: string]: string, ...},
  +search: string,
|};

type TransitionHookResult = boolean | string | null | void;
type TransitionHook = (
  location: Location
) => TransitionHookResult | Promise<TransitionHookResult>;

export type Router = {|
  +push: string => void,
  +replace: string => void,
  +isActive: (href: string, exact?: boolean) => boolean,
  +addTransitionHook: TransitionHook => () => void,
  +RouterLink: React.ComponentType<RouterLinkProps>,
  +location: Location,
|};

export type RouterLinkProps = {|
  +disableSpaHijack: boolean,
  +exact?: boolean,
  +nextRoute?: string,
  +anchorProps: BasicAnchorProps,
|};

function ClassicRouterLink({
  disableSpaHijack,
  exact: _,
  anchorProps,
}: RouterLinkProps) {
  // TODO(dmnd): Try to enable this invariant
  // invariant(!exact, "classic app links do not have active state");
  return (
    // eslint-disable-next-line jsx-a11y/anchor-has-content,react/forbid-elements
    <a
      data-ignore-spa-route={disableSpaHijack ? "true" : undefined}
      {...anchorProps}
    />
  );
}

const BLANK_ROUTER = {
  addTransitionHook: () => () => {},
  isActive: () => false,
  RouterLink: ClassicRouterLink,
  push: () => {},
  replace: () => {},
  // this won't work, but non specialist apps don't use this (yet), and it's basically window.location
  location: {
    pathname: "",
    hash: "",
    search: "",
    query: {},
  },
};

const RouterContext: React.Context<Router> = React.createContext(BLANK_ROUTER);
export default RouterContext;
