import Document, { Head, Main, NextScript } from 'next/document';
import { getNextConfig } from "$ustoreinternal/services/utils";
import { createHeadSection } from '$ustoreinternal/services/headSection'
import { createVersionedFooterScripts } from '$ustoreinternal/services/footerScriptsSection'

export default class MyDocument extends Document {

  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps };
  }

  render() {
    const { storeScriptUrls, storeStyleUrls } = {}
    const { assetPrefix, buildVersion } = getNextConfig()

    return (
      <html lang="en">
        <Head>
          <script crossOrigin="" src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
          <script crossOrigin="" src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
          <script crossOrigin="" src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js"></script>
          {/* <script src={`${assetPrefix}/static-internal/ie-11-polyfill.js`} /> */}
          <script src={`${assetPrefix}/static-internal/fetch.polyfill.js?rand=${buildVersion || Math.random()}`} />
          <script src={`${assetPrefix}/static-internal/browser-compatibility-check.js?rand=${buildVersion || Math.random()}`} />
          <script src={`${assetPrefix}/static-internal/append-scripts.js?rand=${buildVersion || Math.random()}`} />
          <script src={`/uStoreRestAPI/v1/system/properties/form/widgets?rand=${Math.random()}`}/>
          <script src={`${assetPrefix}/static-internal/append-ducs-scripts.js?rand=${buildVersion || Math.random()}`} />

          {/* this is for supporting a favicon */}
          <link rel="shortcut icon" id='favicon' href='' />

          {createHeadSection(storeStyleUrls, assetPrefix)}
          {/* This is for a future support of ie 11 */}
          {/* <script src={`${assetPrefix}/static-internal/css-vars-ponyfill.min.js`} /> */}
        </Head>
        <body>
          <script src={`${assetPrefix}/static-internal/append-custom-css.js?rand=${buildVersion || Math.random()}`} />
          <script src={`${assetPrefix}/static-internal/append-theme-editor-vars.js?rand=${buildVersion || Math.random()}`} />
          <Main />
          <NextScript />
          {createVersionedFooterScripts(storeScriptUrls, assetPrefix, buildVersion)}
          <iframe id="legacy-iframe" src="about:blank" className="iframe" height="0" />
        </body>
      </html>
    );
  }
}
