import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const originalRenderPage = ctx.renderPage;
    let restClient;

    ctx.renderPage = () =>
      originalRenderPage({
        enhanceApp: App => props => {
          const app = <App {...props} />;
          restClient = app.props.pageProps.client;
          delete app.props.pageProps.client;
          return app;
        },
      });

    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps, restClient };
  }

  render() {
    return (
      <Html lang={this.props.restClient.currentLanguage}>
        <Head prefix="og: http://ogp.me/ns#" />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;
