import { firestore } from "firebase";
import Cards from "./Cards";
import { OrderedMap } from "immutable";
import { DeckModel } from "../../decks";

/**
 * Proxy object to represent a Deck object data.
 * It's used to allow real Deck operations to be performed like it was in
 * a plain object.
 */
class Deck {
  constructor(deckDoc) {
    this._deckDoc = deckDoc;
  }

  getCards() {
    return Cards.getDeckCards(this);
  }

  delete() {
    return this.getCards()
      .deleteAll()
      .then(() => {
        return this._deckDoc.ref.delete();
      });
  }

  update(data) {
    return this._deckDoc.ref.update({
      ...data,
      updateTime: firestore.FieldValue.serverTimestamp()
    });
  }

  asDeckModel() {
    return new DeckModel({
      ...this._deckDoc.data(),
      id: this._deckDoc.id
    });
  }

  asDeepDeckModel() {
    const shallowDeck = this.asDeckModel();

    return this.getCards()
      .getAll()
      .then(cards => {
        return OrderedMap(cards.map(card => card.asCardModel(shallowDeck)));
      })
      .then(cardsModels => {
        return shallowDeck.set("cards", cardsModels);
      });
  }

  get doc() {
    return this._deckDoc;
  }

  get id() {
    return this._deckDoc.id;
  }
}

export default Deck;

@@ -973,21 +973,45 @@
 eredMap(
-cards
+%0A          cards%0A            
 .map(car
@@ -1045,16 +1045,67 @@
 owDeck))
+%0A            .map(card =%3E %5Bcard.id, card%5D)%0A        
 );%0A     

Fix problem with cards instantiation in Deck proxy