import axios from "axios";
import delve from 'dlv';

import SeoConfig from "../../components/seo-config/seo-config";
import Layout from "../../components/layout/layout";

const getPageMetadata = async (path) => {
  try {
    return await axios.get(`${process.env.STRAPI_URL_API}/pages?filters[slug]=${path === '/' ? 'home' : path.slice(1, path.length)}&populate=deep`);
  } catch (e) {
    console.error(e);
    return Promise.resolve({})
  }
}

export const getServerSideProps = async (context) => {
  const path = context.resolvedUrl;
  const menuHeader = await axios.get(`${process.env.STRAPI_URL_API}/menus/1?nested&populate=deep`);
  const menuFooter = await axios.get(`${process.env.STRAPI_URL_API}/menus/2?nested&populate=deep`);
  const page = await getPageMetadata(path);
  return {
    props: {
      menuHeader: delve(menuHeader, 'data.data.attributes.items.data', []),
      menuFooter: delve(menuFooter, 'data.data.attributes.items.data', []),
      page: delve(page, "data.data.0.attributes", {}),
      seo: delve(page, "data.data.0.attributes.seo", {})
    }
  }
}

export function SignupConfirmation({menuHeader, menuFooter, page, seo}) {
  return (
    <>
      <SeoConfig {...seo}/>
      <Layout menuHeader={menuHeader} menuFooter={menuFooter}>
        <p>Page d&apos;accueil</p>
      </Layout>
    </>
  );
}

export default SignupConfirmation;
