import UserController from "../../controllers/UserController";

export const processObjectValuePerms = async <T>(
  obj: T,
  scope: string, // e.g. "unifi.wifi.read"
  user: UserController,
): Promise<Partial<T>> => {
  let result: Partial<T> = {};

  for (const key in obj) {
    if (await user.hasPermission(`${scope}.${key}`)) {
      result[key] = obj[key];
    }
  }

  return result;
};

export const processObjectPermMap = async <T extends Record<string, unknown>>(
  obj: T,
  scope: string,
  user: UserController,
): Promise<Record<keyof T, boolean>> => {
  const result = {} as Record<keyof T, boolean>;

  for (const key in obj) {
    result[key] = await user.hasPermission(`${scope}.${key}`);
  }

  return result;
};
