class AppMetaInitializer {
    private readonly settings: object;

    constructor(settings: object) {
        this.settings = settings;
    }

    public init() {
        console.log('Init app meta');
        document.title = this.settings.app_name;
        this.setMeta('application-name', this.settings.app_name);
        this.setMeta('apple-mobile-web-app-title', this.settings.app_name);
        this.setMeta('mobile-web-app-capable', 'yes');
        this.setMeta('apple-mobile-web-app-capable', 'yes');
        this.setMeta('apple-mobile-web-app-status-bar-style', 'default');
        this.setMeta('theme-color', '#000000');
        this.setMeta('msapplication-navbutton-color', '#000000');
        this.setMeta('apple-mobile-web-app-status-bar-style', 'black-translucent');
        
        // Добавляем apple-touch-icon теги
        if (this.settings.apple_touch_icon) {
            this.addLink('apple-touch-icon', this.settings.apple_touch_icon);
        } else {
            // Добавляем дефолтный apple-touch-icon, если не указан
            this.addLink('apple-touch-icon', '/favicon.ico');
        }
    }

    private setMeta(name: string, content: string) {
        let meta = document.querySelector(`meta[name="${name}"]`);
        if (!meta) {
            meta = document.createElement('meta');
            meta.setAttribute('name', name);
            document.head.appendChild(meta);
        }
        meta.setAttribute('content', content);
    }

    private addLink(rel: string, href: string, sizes?: string) {
        const link = document.createElement('link');
        link.rel = rel;
        link.href = href;
        if (sizes) link.sizes = sizes;
        document.head.appendChild(link);
    }
}

export default AppMetaInitializer;