import MyUserManager from '@user/MyUserManager';
import {filterParam} from './get';

export type databaseNames = 'users' | 'chat' | 'chatRoomInfos' | 'events';
export type possibleDBKeys = string;

export interface databaseConf<props, enums> {
  filePath: string;
  version: number;
  migration?: any;
  keys: enums;
  getEntry: (key: possibleDBKeys, suffix?: string) => Promise<props | null>;
  getAllEntries: (
    filter?: filterParam,
    suffix?: string,
  ) => Promise<props[] | null>;
  setEntry: (val: props, suffix?: string) => Promise<void>;
  defaultProps: props;
  details: {
    name: databaseNames;
    properties: {[key in keyof props]: string};
    primaryKey: keyof props;
  };
}

export interface databaseNameSuffix {
  name: databaseNames;
  suffix?: string;
}

export function mergeDBName(nameObj: databaseNameSuffix, web?: 'web'): string {
  if (web === 'web') {
    return nameObj.suffix === undefined
      ? nameObj.name + '-' + MyUserManager.getSelectedUserAccount()
      : nameObj.name +
          '-' +
          MyUserManager.getSelectedUserAccount() +
          ('_' + nameObj.suffix);
  }

  return nameObj.suffix === undefined
    ? nameObj.name
    : nameObj.name + nameObj.suffix;
}
