import { List, Record } from 'immutable';

import Post from './Post.js';
import TicketClass from './TicketClass.js';

export default class Event extends Record({
  id: null,
  isTicketed: false,
  requireLoginToSeeAuction: false,
  description: null,
  endTime: null,
  images: new List(),
  posts: new List(),
  showFrom: null,
  showUntil: null,
  startTime: null,
  tagline: null,
  title: null,
  url: null,
  ticketClasses: new List(),
}) {
  get isSoldOut() {
    if (this.isTicketed) {
      return false;
    }

    return this.ticketClasses.find(t => t.available > 0) || false;
  }
}

Event.fromJS = (data = {}) => {
  return new Event({
    id: data._id,
    ...data,
    images: new List(data.images),
    posts: new List(data.posts.map(p => Post.fromJS(p))),
    ticketClasses: new List(data.ticketClasses.map(t => TicketClass.fromJS(t))),
  });
};
