import { Asset } from "cc";
import { SpriteFrame } from "cc";
import { Prefab, resources } from "cc";

export default class AB {
  async loadInLoading(): Promise<boolean> {
    return new Promise((resolve, reject) => {
      resolve(true);
    });
  }
  loadRes(path: string, asset: typeof Asset): Promise<Asset> {
    return new Promise((resolve, reject) => {
      resources.load(path, asset, (err, asset) => {
        if (err) {
          reject(err);
        } else {
          resolve(asset);
        }
      });
    });
  }
  private static _ins: AB;

  public static get ins(): AB {
    return AB._ins ??= new AB();
  }

  async loadPrefab(path): Promise<Prefab> {
    return new Promise((resolve, reject) => {
      resources.load(path, Prefab, (err, prefab) => {
        if (err) {
          reject(err);
        } else {
          resolve(prefab);
        }
      });
    });
  }

  async loadSpriteFrame(path: string): Promise<SpriteFrame> {
    let a = await this.loadRes(path+"/spriteFrame", SpriteFrame);
    return a as SpriteFrame;
  }
}
