'use strict';

const BasePage = (function () {
    function BasePage() {
        this.container = document.getElementById('app');
        this.components = [];
        this.orgHeader = null;
    }

    BasePage.prototype.mount = function () {
        if (!this.container) {
            console.error('App container not found');
            return;
        }

        // Create wrapper with OrgHeader and page content
        const wrapper = document.createElement('div');
        wrapper.className = 'page-wrapper';

        // Create and mount OrgHeader
        const headerContainer = document.createElement('div');
        wrapper.appendChild(headerContainer);
        this.orgHeader = new OrgHeader(headerContainer, this.getOrgHeaderConfig());
        this.orgHeader.mount();

        // Create page content container
        const pageContent = document.createElement('div');
        pageContent.className = 'page-content';
        pageContent.innerHTML = this.render();
        wrapper.appendChild(pageContent);

        this.container.innerHTML = '';
        this.container.appendChild(wrapper);

        this.afterMount();
    };

    BasePage.prototype.getOrgHeaderConfig = function () {
        // Override this method to provide custom config
        return {
            reputation: 1250,
            funds: 45000
        };
    };

    BasePage.prototype.render = function () {
        throw new Error('render() must be implemented');
    };

    BasePage.prototype.afterMount = function () {
        // Override to initialize components after render
    };

    BasePage.prototype.destroy = function () {
        if (this.orgHeader && this.orgHeader.destroy) {
            this.orgHeader.destroy();
        }
        this.orgHeader = null;

        for (let i = 0; i < this.components.length; i++) {
            if (this.components[i].destroy) {
                this.components[i].destroy();
            }
        }
        this.components = [];

        if (this.container) {
            this.container.innerHTML = '';
        }
    };

    return BasePage;
})();
