/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict
 * @format
 */

/*
There's no way for us to automatically reference the transform function types generated by `genTransformNodeTypes`
*/
import {
  GetTransformESTreeJSON,
  TransformESTreePackage,
  TransformPackage,
  formatAndWriteDistArtifact,
} from './utils/scriptUtils';

const imports: Array<string> = [];
const cloneSignatures: Array<string> = [];

const signatureNames: Array<string> = [];
for (const node of GetTransformESTreeJSON()) {
  imports.push(node.name);
  const signatureName = `${node.name}CloneSignature`;
  signatureNames.push(signatureName);

  const propTypes = `${node.name}Props`;
  cloneSignatures.push(
    `
type ${signatureName} =
  ((
    node: ${node.name},
    newProps: Partial<${propTypes}>,
  ) => DetachedNode<${node.name}>) &
  ((
    node: ?${node.name},
    newProps: Partial<${propTypes}>,
  ) => DetachedNode<${node.name}> | null);
`,
  );
}

const fileContents = `\
import type {
${imports.join(',\n')}
} from '${TransformESTreePackage}';
import type {
${imports.map(i => `${i}Props`).join(',\n')}
} from './node-types';
import type {DetachedNode} from '../detachedNode';

${cloneSignatures.join(';\n')};
export type TransformCloneSignatures = ${signatureNames.join(' & ')};
`;

formatAndWriteDistArtifact({
  code: fileContents,
  package: TransformPackage,
  file: 'generated/TransformCloneSignatures.js.flow',
  flow: 'strict-local',
});
