16a5c065cSBartosz Kaszubowskiimport { renderMethod } from './APISectionMethods';
26a5c065cSBartosz Kaszubowski
3f8204ef0SBartosz Kaszubowskiimport { APIDataType } from '~/components/plugins/api/APIDataType';
4abded2bbSBartosz Kaszubowskiimport {
5abded2bbSBartosz Kaszubowski  CommentData,
6abded2bbSBartosz Kaszubowski  InterfaceDefinitionData,
7abded2bbSBartosz Kaszubowski  MethodSignatureData,
82c8d37c0SBartosz Kaszubowski  PropData,
9abded2bbSBartosz Kaszubowski} from '~/components/plugins/api/APIDataTypes';
1025b16883SBartosz Kaszubowskiimport { APISectionDeprecationNote } from '~/components/plugins/api/APISectionDeprecationNote';
11b3bd70ceSTomasz Sapetaimport { APISectionPlatformTags } from '~/components/plugins/api/APISectionPlatformTags';
12b671e53fSBartosz Kaszubowskiimport {
13b671e53fSBartosz Kaszubowski  CommentTextBlock,
1440194603SBartosz Kaszubowski  getTagData,
157d500c47SBartosz Kaszubowski  parseCommentContent,
16b15ebaf2SBartosz Kaszubowski  renderFlags,
177534da61SBartosz Kaszubowski  renderParamRow,
18a0c5fbc5SBartosz Kaszubowski  ParamsTableHeadRow,
19b671e53fSBartosz Kaszubowski  resolveTypeName,
207d500c47SBartosz Kaszubowski  renderDefaultValue,
216a5c065cSBartosz Kaszubowski  STYLES_APIBOX,
22c4c6b9d1SBartosz Kaszubowski  getTagNamesList,
2312abeb84SBartosz Kaszubowski  STYLES_APIBOX_NESTED,
24*f4b1168bSBartosz Kaszubowski  ELEMENT_SPACING,
25a16a3d18SBartosz Kaszubowski  H3Code,
265990cc31SBartosz Kaszubowski  getCommentContent,
27be43ea08SBartosz Kaszubowski  BoxSectionHeader,
28b671e53fSBartosz Kaszubowski} from '~/components/plugins/api/APISectionUtils';
297534da61SBartosz Kaszubowskiimport { Cell, Row, Table } from '~/ui/components/Table';
303324c13cSBartosz Kaszubowskiimport { H2, BOLD, P, CODE, DEMI, MONOSPACE } from '~/ui/components/Text';
31299f02f2SBartosz Kaszubowski
32299f02f2SBartosz Kaszubowskiexport type APISectionInterfacesProps = {
33299f02f2SBartosz Kaszubowski  data: InterfaceDefinitionData[];
34299f02f2SBartosz Kaszubowski};
35299f02f2SBartosz Kaszubowski
367d500c47SBartosz Kaszubowskiconst renderInterfaceComment = (
377d500c47SBartosz Kaszubowski  comment?: CommentData,
387d500c47SBartosz Kaszubowski  signatures?: MethodSignatureData[],
397d500c47SBartosz Kaszubowski  defaultValue?: string
407d500c47SBartosz Kaszubowski) => {
41abded2bbSBartosz Kaszubowski  if (signatures && signatures.length) {
42b15ebaf2SBartosz Kaszubowski    const { type, parameters, comment: signatureComment } = signatures[0];
435990cc31SBartosz Kaszubowski    const defaultTag = getTagData('default', signatureComment);
445990cc31SBartosz Kaszubowski    const initValue =
455990cc31SBartosz Kaszubowski      defaultValue || (defaultTag ? getCommentContent(defaultTag.content) : undefined);
46abded2bbSBartosz Kaszubowski    return (
47abded2bbSBartosz Kaszubowski      <>
487534da61SBartosz Kaszubowski        {parameters?.length ? parameters.map(param => renderParamRow(param)) : null}
49be43ea08SBartosz Kaszubowski        <DEMI>Returns</DEMI>
5012abeb84SBartosz Kaszubowski        <CODE>{resolveTypeName(type)}</CODE>
51b15ebaf2SBartosz Kaszubowski        {signatureComment && (
52a4820d5fSBartosz Kaszubowski          <>
53a4820d5fSBartosz Kaszubowski            <br />
54d25015daSBartosz Kaszubowski            <APISectionDeprecationNote comment={comment} />
55669ac6e6SBartosz Kaszubowski            <CommentTextBlock
56f6311d0dSBartosz Kaszubowski              inlineHeaders
57669ac6e6SBartosz Kaszubowski              comment={signatureComment}
587d500c47SBartosz Kaszubowski              afterContent={renderDefaultValue(initValue)}
59669ac6e6SBartosz Kaszubowski            />
60a4820d5fSBartosz Kaszubowski          </>
61b15ebaf2SBartosz Kaszubowski        )}
62abded2bbSBartosz Kaszubowski      </>
63abded2bbSBartosz Kaszubowski    );
64abded2bbSBartosz Kaszubowski  } else {
655990cc31SBartosz Kaszubowski    const defaultTag = getTagData('default', comment);
665990cc31SBartosz Kaszubowski    const initValue =
675990cc31SBartosz Kaszubowski      defaultValue || (defaultTag ? getCommentContent(defaultTag.content) : undefined);
687534da61SBartosz Kaszubowski    return (
69d25015daSBartosz Kaszubowski      <>
70d25015daSBartosz Kaszubowski        <APISectionDeprecationNote comment={comment} />
71669ac6e6SBartosz Kaszubowski        <CommentTextBlock
72669ac6e6SBartosz Kaszubowski          comment={comment}
737d500c47SBartosz Kaszubowski          afterContent={renderDefaultValue(initValue)}
747534da61SBartosz Kaszubowski          emptyCommentFallback="-"
75669ac6e6SBartosz Kaszubowski        />
76d25015daSBartosz Kaszubowski      </>
7740194603SBartosz Kaszubowski    );
78abded2bbSBartosz Kaszubowski  }
79abded2bbSBartosz Kaszubowski};
80abded2bbSBartosz Kaszubowski
81b671e53fSBartosz Kaszubowskiconst renderInterfacePropertyRow = ({
82b671e53fSBartosz Kaszubowski  name,
83b671e53fSBartosz Kaszubowski  flags,
84b671e53fSBartosz Kaszubowski  type,
85b671e53fSBartosz Kaszubowski  comment,
86abded2bbSBartosz Kaszubowski  signatures,
877d500c47SBartosz Kaszubowski  defaultValue,
886a5c065cSBartosz Kaszubowski}: PropData): JSX.Element => {
895990cc31SBartosz Kaszubowski  const defaultTag = getTagData('default', comment);
905990cc31SBartosz Kaszubowski  const initValue = parseCommentContent(
915990cc31SBartosz Kaszubowski    defaultValue || (defaultTag ? getCommentContent(defaultTag.content) : '')
925990cc31SBartosz Kaszubowski  );
936a5c065cSBartosz Kaszubowski  return (
94af644ed4SBartosz Kaszubowski    <Row key={name}>
95821bddc5SBartosz Kaszubowski      <Cell fitContent>
9612abeb84SBartosz Kaszubowski        <BOLD>{name}</BOLD>
977d500c47SBartosz Kaszubowski        {renderFlags(flags, initValue)}
98af644ed4SBartosz Kaszubowski      </Cell>
996a5c065cSBartosz Kaszubowski      <Cell fitContent>
100f8204ef0SBartosz Kaszubowski        <APIDataType typeDefinition={type} />
1016a5c065cSBartosz Kaszubowski      </Cell>
1027d500c47SBartosz Kaszubowski      <Cell fitContent>{renderInterfaceComment(comment, signatures, initValue)}</Cell>
103af644ed4SBartosz Kaszubowski    </Row>
104b671e53fSBartosz Kaszubowski  );
1056a5c065cSBartosz Kaszubowski};
106b671e53fSBartosz Kaszubowski
107bb49777fSBartosz Kaszubowskiconst renderInterface = ({
108bb49777fSBartosz Kaszubowski  name,
109bb49777fSBartosz Kaszubowski  children,
110bb49777fSBartosz Kaszubowski  comment,
1111886d870SBartosz Kaszubowski  extendedTypes,
1126a5c065cSBartosz Kaszubowski}: InterfaceDefinitionData): JSX.Element | null => {
1136a5c065cSBartosz Kaszubowski  const interfaceChildren = children?.filter(child => !child?.inheritedFrom) || [];
1146a5c065cSBartosz Kaszubowski
1156a5c065cSBartosz Kaszubowski  if (!interfaceChildren.length) return null;
1166a5c065cSBartosz Kaszubowski
1176a5c065cSBartosz Kaszubowski  const interfaceMethods = interfaceChildren.filter(child => child.signatures);
1186a5c065cSBartosz Kaszubowski  const interfaceFields = interfaceChildren.filter(child => !child.signatures);
1196a5c065cSBartosz Kaszubowski
1206a5c065cSBartosz Kaszubowski  return (
12112abeb84SBartosz Kaszubowski    <div key={`interface-definition-${name}`} css={[STYLES_APIBOX, STYLES_APIBOX_NESTED]}>
12225b16883SBartosz Kaszubowski      <APISectionDeprecationNote comment={comment} />
123d9bd5b6cSBartosz Kaszubowski      <APISectionPlatformTags comment={comment} prefix="Only for:" />
124c4c6b9d1SBartosz Kaszubowski      <H3Code tags={getTagNamesList(comment)}>
1253324c13cSBartosz Kaszubowski        <MONOSPACE weight="medium">{name}</MONOSPACE>
126299f02f2SBartosz Kaszubowski      </H3Code>
1277859b985SBartosz Kaszubowski      {extendedTypes?.length ? (
128*f4b1168bSBartosz Kaszubowski        <P className={ELEMENT_SPACING}>
129be43ea08SBartosz Kaszubowski          <DEMI>Extends: </DEMI>
1301886d870SBartosz Kaszubowski          {extendedTypes.map(extendedType => (
13112abeb84SBartosz Kaszubowski            <CODE key={`extend-${extendedType.name}`}>{resolveTypeName(extendedType)}</CODE>
1321886d870SBartosz Kaszubowski          ))}
1331886d870SBartosz Kaszubowski        </P>
1347859b985SBartosz Kaszubowski      ) : null}
13525b16883SBartosz Kaszubowski      <CommentTextBlock comment={comment} includePlatforms={false} />
1366a5c065cSBartosz Kaszubowski      {interfaceMethods.length ? (
1376a5c065cSBartosz Kaszubowski        <>
138be43ea08SBartosz Kaszubowski          <BoxSectionHeader text={`${name} Methods`} />
1394c73bbf6SBartosz Kaszubowski          {interfaceMethods.map(method => renderMethod(method, { exposeInSidebar: false }))}
1406a5c065cSBartosz Kaszubowski        </>
1416a5c065cSBartosz Kaszubowski      ) : undefined}
1426a5c065cSBartosz Kaszubowski      {interfaceFields.length ? (
1436a5c065cSBartosz Kaszubowski        <>
144be43ea08SBartosz Kaszubowski          <BoxSectionHeader text={`${name} Properties`} />
145af644ed4SBartosz Kaszubowski          <Table>
146a0c5fbc5SBartosz Kaszubowski            <ParamsTableHeadRow />
1476a5c065cSBartosz Kaszubowski            <tbody>{interfaceFields.map(renderInterfacePropertyRow)}</tbody>
148af644ed4SBartosz Kaszubowski          </Table>
149a16a3d18SBartosz Kaszubowski          <br />
1506a5c065cSBartosz Kaszubowski        </>
1516a5c065cSBartosz Kaszubowski      ) : undefined}
152299f02f2SBartosz Kaszubowski    </div>
1536a5c065cSBartosz Kaszubowski  );
1546a5c065cSBartosz Kaszubowski};
155299f02f2SBartosz Kaszubowski
156558a63feSBartosz Kaszubowskiconst APISectionInterfaces = ({ data }: APISectionInterfacesProps) =>
157299f02f2SBartosz Kaszubowski  data?.length ? (
158299f02f2SBartosz Kaszubowski    <>
159299f02f2SBartosz Kaszubowski      <H2 key="interfaces-header">Interfaces</H2>
160299f02f2SBartosz Kaszubowski      {data.map(renderInterface)}
161299f02f2SBartosz Kaszubowski    </>
162299f02f2SBartosz Kaszubowski  ) : null;
163299f02f2SBartosz Kaszubowski
164299f02f2SBartosz Kaszubowskiexport default APISectionInterfaces;
165