import { notFound } from '@dvn/dvn-mock/api/utils';
import { STORAGE_KEY_KIMLIK_USER_ID } from '@dvn/kimlik/api/kimlikApi';
export class TakvimApiDemo {
  constructor(database, storage, logger) {
    this.logger = logger.new('TakvimApiDemo').setColor('asphalt');
    this.storage = storage;
    this.database = database;
  }
  getAvailabilities(availabilityConfigId) {
    const userId = this.storage.get(STORAGE_KEY_KIMLIK_USER_ID);
    const user = this.database.kimlikUsers[userId];
    if (!user) {
      this.logger.warnGroup('getAvailabilities: user not found', { userId, availabilityConfigId });
      return Promise.reject(notFound());
    }
    const table = this.database[availabilityConfigId];
    if (!table) {
      this.logger.warnGroup('getAvailabilities: table not found', { availabilityConfigId });
      return Promise.reject(`TakvimApiDemo.getAvailabilities: Table "${availabilityConfigId}" not found`);
    }
    const items = Object.keys(table).map(id => table[id]);
    const results = items.filter(item => item.user._id === userId);
    this.logger.debugGroup('getAvailabilities', { availabilityConfigId, results });
    return Promise.resolve({ results });
  }
}
