import {Component, isDevMode} from '@angular/core';
import {select, Store} from "@ngrx/store";
import {fromApplication, fromAuthorize, selectIsLoggedIn} from "./shared/state/store";
import {environment} from "../environments/environment";

// @ts-ignore
import packageJson from '../../../../package.json';
import {ThemeService} from "./shared/services/theme.service";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    appName = environment.appName;


    constructor(private store: Store, public themeService: ThemeService) {
        const darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
        if (darkMode) {
            this.themeService.setTheme('dark');
        }
        this.store.dispatch(fromAuthorize.loadLocalStorage());
        this.store.pipe(
            select(selectIsLoggedIn),
        ).subscribe((loggedIn) => {
            if (loggedIn && !isDevMode()) {
                this.store.dispatch(fromApplication.loadLists());
                this.store.dispatch(fromApplication.loadFollowings());
            }
        });
    }
}
