/*
/* (c) 2016 Björn Ricks <bjoern.ricks@gmail.com>
/*
/* See LICENSE comming with the source of daap.js for details.
*/

import {is_defined} from './utils.js';

import Song from './song.js';

export class Items {

    constructor(data, db_id, session_id, url) {
        this._data = data;
        this._db_id = db_id;
        this._session_id = session_id;
        this._url = url;
        let items = data.find('mlcl');
        this._song = items.find('mlit');
        this._length = data.get('mrco');
    }

    get length() {
        return this._length;
    }

    get(max) {
        let results = [];

        for (let i = 0; is_defined(this._song) && this._song.isValid() &&
                (!is_defined(max) || i < max); i++) {
            results.push(new Song(this._song, this._db_id, this._session_id,
                this._url));
            this._song = this._song.next();
        }

        return results;
    }

    [Symbol.iterator]() {
        return this;
    }

    next() {
        let song = this._song;
        let has_song = is_defined(song) && song.isValid();
        this._song = this._song.next();
        return {
            done: !has_song,
            value: has_song ?
            new Song(song, this._db_id, this._session_id, this._url) :
            undefined,
        };
    }
}

export default Items;

// vim: set ts=4 sw=4 tw=80:

@@ -158,20 +158,19 @@
 s';%0A
-%0A
 import 
-Song
+Item
  fro
@@ -174,20 +174,20 @@
 from './
-song
+item
 .js';%0A%0Ae
@@ -251,16 +251,30 @@
 _id, url
+, clazz = Item
 ) %7B%0A    
@@ -442,20 +442,20 @@
   this._
-song
+item
  = items
@@ -487,33 +487,21 @@
 is._
-length = data.get('mrco')
+clazz = clazz
 ;%0A  
@@ -545,22 +545,32 @@
 n this._
-length
+data.get('mrco')
 ;%0A    %7D%0A
@@ -653,20 +653,20 @@
 d(this._
-song
+item
 ) && thi
@@ -668,20 +668,20 @@
 & this._
-song
+item
 .isValid
@@ -773,44 +773,37 @@
 new 
-Song(this._song, 
+this.clazz(
 this._
-db_id
+item
 , this._
 sess
@@ -794,31 +794,26 @@
 item, this._
-session
+db
 _id,%0A       
@@ -816,24 +816,42 @@
             
+ this._session_id,
  this._url))
@@ -862,36 +862,36 @@
           this._
-song
+item
  = this._song.ne
@@ -875,36 +875,36 @@
 s._item = this._
-song
+item
 .next();%0A       
@@ -1017,20 +1017,20 @@
     let 
-song
+item
  = this.
@@ -1030,20 +1030,20 @@
 = this._
-song
+item
 ;%0A      
@@ -1052,20 +1052,20 @@
 let has_
-song
+item
  = is_de
@@ -1074,21 +1074,21 @@
 ned(
-song
+item
 ) && 
-song
+item
 .isV
@@ -1109,20 +1109,20 @@
   this._
-song
+item
  = this.
@@ -1122,20 +1122,20 @@
 = this._
-song
+item
 .next();
@@ -1175,20 +1175,20 @@
 e: !has_
-song
+item
 ,%0A      
@@ -1204,20 +1204,20 @@
 ue: has_
-song
+item
  ?%0A     
@@ -1231,17 +1231,23 @@
 new 
-Song(song
+this.clazz(item
 , th

Generalize Items class to support all Item classes