import { getCurrentInstance } from "vue";
import store from "store";
import { CoreConfig } from "../model/domain/CoreConfig";
import { NAMESPACE } from "../constants/config/storage.config";
import { AppContext } from "@vue/runtime-core";

class RouteTopologyStore {
  private appContext: AppContext | undefined;
  private static readonly INITED = NAMESPACE + "inited";
  private static readonly TREE_ROUTES = NAMESPACE + "treeRoutes";
  private static readonly HORIZONTAL_ROUTES = NAMESPACE + "horizontalRoutes";

  /**
   * @description: 获取全局配置
   * @author ChenRui
   * @date 2022/5/25 17:55
   */
  get coreConfig(): Partial<CoreConfig> {
    if (!this.appContext) {
      this.appContext = getCurrentInstance()?.appContext;
    }
    if (this.appContext && this.appContext.config.globalProperties.$dc != null) {
      return this.appContext.config.globalProperties.$dc;
    }
    return {};
  }

  get suffix(): string {
    if (this.coreConfig.sysId) {
      return ":" + this.coreConfig.sysId;
    }
    return "";
  }

  public set inited(inited: boolean) {
    const key = RouteTopologyStore.INITED + this.suffix;
    if (inited) {
      store.set(key, inited);
    } else {
      store.remove(key);
    }
  }

  public get inited(): boolean {
    const key = RouteTopologyStore.INITED + this.suffix;
    return store.get(key);
  }

  public set treeRoutes(treeRoutes: any) {
    const key = RouteTopologyStore.TREE_ROUTES + this.suffix;
    if (treeRoutes) {
      store.set(key, treeRoutes);
    } else {
      store.remove(key);
    }
  }

  public get treeRoutes(): any {
    const key = RouteTopologyStore.TREE_ROUTES + this.suffix;
    return store.get(key);
  }

  public set horizontalRoutes(horizontalRoutes: any) {
    const key = RouteTopologyStore.HORIZONTAL_ROUTES + this.suffix;
    if (horizontalRoutes) {
      store.set(key, horizontalRoutes);
    } else {
      store.remove(key);
    }
  }

  public get horizontalRoutes(): any {
    const key = RouteTopologyStore.HORIZONTAL_ROUTES + this.suffix;
    return store.get(key);
  }
}
const routeTopologyStore = new RouteTopologyStore();
export { routeTopologyStore };
