/**
 * 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 modifySignatures: Array<string> = [];

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

  const propTypes = `${node.name}Props`;
  modifySignatures.push(
    `
type ${signatureName} = (
  node: ?${node.name},
  newProps: Partial<${propTypes}>,
) => void;
`,
  );
}

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

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

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