import { makeAutoObservable } from 'mobx';

import { rpc } from '../api/request';
import {
  ModeFWEnum,
  ShmChannelType,
} from '../api/types';

class Store {
    constructor() {
        makeAutoObservable(this);
    }


    channelTab: "member" | "join" = "member";

    setChannelTab(channelTab: "member" | "join") {
        this.channelTab = channelTab;
    }

    mode: ModeFWEnum = "widget";

    setMode(mode: ModeFWEnum) {
        this.mode = mode;
    }

    isFixedChannel: boolean = false;

    setIsFixedChannel(isFixedChannel: boolean) {
        this.isFixedChannel = isFixedChannel;
    }



    selectChannelID: string | null = null;
    setSelectChannelID(channelID: string | null) {
        this.selectChannelID = channelID;
    }

    channelList: ShmChannelType[] = [];

    userID: string | null = null;

    loadUser() {
        rpc.profileID().then(({ result: data }) => {

            this.userID = data ?? null;
        });
    }



    isMobile = () => {
        return window.innerWidth < 768 || this.cap;
    }

    //Проверка на Capacitor
    cap: boolean = false;

    setCap(cap: boolean) {
        this.cap = cap;
    }



    setChannelList(channels: ShmChannelType[]) {
        this.channelList = channels;
    }

    joinChannelList: ShmChannelType[] = [];

    setJoinChannelList(channels: ShmChannelType[]) {
        this.joinChannelList = channels;
    }

    loadJoinChannelList() {
        return new Promise((resolve, reject) => {
            rpc.joinableChannels({}, ["shmChannelMessages"]).then(({ result: data }) => {
                this.setJoinChannelList(data ?? []);
            }).finally(() => {
                resolve(true);
            });
        });
    }

    loadChannelList() {
        return new Promise((resolve, reject) => {
            rpc.channelList({}, ["shmChannelMessages"]).then(({ result: data }) => {
                this.setChannelList(data?.data ?? []);
            }).finally(() => {
                resolve(true);
            });
        });


    }

}

export default new Store();
