import React from 'react'
import PropTypes from 'prop-types'
import PublicLayout from './templates/public'
import PrivateLayout from './templates/private'
import Main from './partials/main'
import Section from './partials/section'

class Layout extends React.Component {
    static Public = PublicLayout
    static Private = PrivateLayout
    static Main = Main
    static Section = Section

    get layoutTemplate() {
        return this.props.isPrivate ? Layout.Private : Layout.Public
    }

    render() {
        const {isPrivate, policy, ...props} = this.props

        if (!this.props.children) {
            return null
        }

        // Don't show any layout to the user if our policy isn't ready
        if (!policy.isReady || (!policy.isAuthenticated && isPrivate)) {
            return this.props.children
        }

        const LayoutTemplate = this.layoutTemplate

        return <LayoutTemplate {...props}>{this.props.children}</LayoutTemplate>
    }
}

Layout.propTypes = {
    children: PropTypes.node,
    isPrivate: PropTypes.bool,
    policy: PropTypes.object,
}

Layout.defaultProps = {
    isPrivate: false,
}

export {Section}

export default Layout
