// @flow
import type { Livedoc } from './livedoc';
import type { ResponseMetaData } from './playground';

export type LoaderState = {
  +loading: boolean,
  +loadingError: ?string,
  +url: ?string
}
export const newLoaderState = () => ({
  loading: false,
  loadingError: null,
  url: computeInitialUrl(),
});

function computeInitialUrl(): string {
  const url = new URL(window.location.href);
  const specifiedUrl = url.searchParams.get('url');
  if (specifiedUrl) {
    return specifiedUrl;
  }
  return window.location.origin + '/jsondoc';
}

export type PlaygroundState = {
  +waitingResponse: boolean,
  +streamingResponse: boolean,
  +responseMeta: ?ResponseMetaData,
  +responseBody: ?string,
  +error: any,
}
export function newPlaygroundState(): PlaygroundState {
  return {
    waitingResponse: false,
    streamingResponse: false,
    responseMeta: null,
    responseBody: null,
    error: null,
  };
}

export type State = {
  +loader: LoaderState,
  +livedoc: ?Livedoc,
  +playground: PlaygroundState,
}
export const newState = () => ({
  loader: newLoaderState(),
  livedoc: null,
  playground: newPlaygroundState(),
});

// @flow
import type { Livedoc } from './livedoc';
import type { ResponseMetaData } from './playground';

export type LoaderState = {
  +loading: boolean,
  +loadingError: ?string,
  +url: ?string
}
export const newLoaderState = () => ({
  loading: false,
  loadingError: null,
  url: computeInitialUrl(),
});

function computeInitialUrl(): string {
  const url = new URL(window.location.href);
  const specifiedUrl = url.searchParams.get('url');
  if (specifiedUrl) {
    return specifiedUrl;
  }
  // if using the webjar, then the doc endpoint in on the same server at /jsondoc, so "/jsondoc" is sufficient
  // if using an independent UI, then it is likely that the app server URL ends in /jsondoc, so this string helps anyway
  return '/jsondoc';
}

export type PlaygroundState = {
  +waitingResponse: boolean,
  +streamingResponse: boolean,
  +responseMeta: ?ResponseMetaData,
  +responseBody: ?string,
  +error: any,
}
export function newPlaygroundState(): PlaygroundState {
  return {
    waitingResponse: false,
    streamingResponse: false,
    responseMeta: null,
    responseBody: null,
    error: null,
  };
}

export type State = {
  +loader: LoaderState,
  +livedoc: ?Livedoc,
  +playground: PlaygroundState,
}
export const newState = () => ({
  loader: newLoaderState(),
  livedoc: null,
  playground: newPlaygroundState(),
});

Remove host in default doc fetch URL
Remove host in default doc fetch URL

This host is unnecessary because it is equal to the current host, which
is equivalent to not specifying it at all.
If using the webjar, then the doc endpoint in on the same server at
/jsondoc, so "/jsondoc" is sufficient for the fetch to work.
If using an independent UI, then it is likely that the app server URL
ends in /jsondoc, so this string helps anyway because the user just has
to prepend the host.
