/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @emails oncall+relay
 * @flow strict-local
 * @format
 */

'use strict';

import type {
  EntryPoint,
  EntryPointProps,
  PreloadedEntryPoint,
} from '../../EntryPointTypes.flow';
import type {JSResourceReference} from 'JSResourceReference';

const mockJSResource = <TModule>(
  module: TModule,
): JSResourceReference<TModule> => ({
  getModuleId(): string {
    return '';
  },
  getModuleIfRequired(): ?TModule {
    return module;
  },
  load(): Promise<TModule> {
    return Promise.resolve(module);
  },
});

type NestedEntrypointComponentProps = EntryPointProps<{}>;

const NestedEntrypointComponent = (_props: NestedEntrypointComponentProps) =>
  null;

type NestedEntrypointPreloadParams = $ReadOnly<{|
  subEntrypointPreloadParam: string,
|}>;

type NestedEntryPointType = EntryPoint<
  NestedEntrypointPreloadParams,
  typeof NestedEntrypointComponent,
>;

const NestedEntryPoint = ({
  getPreloadProps({subEntrypointPreloadParam: _subEntrypointPreloadParam}) {
    return {};
  },
  root: mockJSResource(NestedEntrypointComponent),
}: NestedEntryPointType);

// Define the parent entrypoint's component

type PreloadedEntrypoints = $ReadOnly<{
  nestedComponent: PreloadedEntryPoint<typeof NestedEntrypointComponent>,
}>;

type ParentEntrypointComponentProps = EntryPointProps<{}, PreloadedEntrypoints>;

const ParentEntrypointComponent = (_props: ParentEntrypointComponentProps) =>
  null;

/**
 * Create a parent entrypoint with the component with
 * INCORRECT params passed into the nested entrypoints.
 */

type BadParentEntrypointParams = $ReadOnly<{}>;

({
  getPreloadProps(_params: BadParentEntrypointParams) {
    return {
      entryPoints: {
        nestedComponent: {
          entryPoint: NestedEntryPoint,
          /**
           $FlowExpectedError The entryPointParams here should be of type
            NestedEntrypointPreloadParams, but it does not contain subEntrypointPreloadParam
          */
          entryPointParams: Object.freeze({}),
        },
      },
    };
  },
  root: mockJSResource(ParentEntrypointComponent),
}: EntryPoint<BadParentEntrypointParams, typeof ParentEntrypointComponent>);

/**
 * Create a parent entrypoint with the component with
 * CORRECT params passed into the nested entrypoints.
 */

type GoodParentEntrypointParams = $ReadOnly<{}>;

({
  getPreloadProps(_params: GoodParentEntrypointParams) {
    return {
      entryPoints: {
        nestedComponent: {
          entryPoint: NestedEntryPoint,
          // No flow error since this matches NestedEntrypointPreloadParams
          entryPointParams: {
            subEntrypointPreloadParam: 'test',
          },
        },
      },
    };
  },
  root: mockJSResource(ParentEntrypointComponent),
}: EntryPoint<GoodParentEntrypointParams, typeof ParentEntrypointComponent>);

@@ -428,21 +428,32 @@
 ence';%0A%0A
-const
+declare function
  mockJSR
@@ -459,19 +459,16 @@
 Resource
- = 
 %3CTModule
@@ -524,193 +524,8 @@
 ule%3E
- =%3E (%7B%0A  getModuleId(): string %7B%0A    return '';%0A  %7D,%0A  getModuleIfRequired(): ?TModule %7B%0A    return module;%0A  %7D,%0A  load(): Promise%3CTModule%3E %7B%0A    return Promise.resolve(module);%0A  %7D,%0A%7D)
 ;%0A%0At

Fix broken flowtest