/** @file MOMStringCollection.cpp
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#include "config.h"

#if ENABLE(RMAI)

#include "MOMStringCollection.h"

#include "UtilsMOM.h"

namespace WebCore {

PassRefPtr<MOMStringCollection> MOMStringCollection::create(mom_urilist strings)
{
    return adoptRef(new MOMStringCollection(strings));
}

PassRefPtr<MOMStringCollection> MOMStringCollection::createAdopt(mom_urilist strings)
{
    RefPtr<MOMStringCollection> list = MOMStringCollection::create(strings);
    mom_urilist_dispose(strings);
    return list.release();        
}

MOMStringCollection::MOMStringCollection(mom_urilist strings)
{
    m_strings = strings ? mom_urilist_keep(strings) : NULL;
}

MOMStringCollection::~MOMStringCollection()
{
    if (m_strings) {
        mom_urilist_dispose(m_strings);
        m_strings = NULL;
    }
}

int MOMStringCollection::length() const
{
    return m_strings ? mom_urilist_count(m_strings) : 0;
}

String MOMStringCollection::item( int index )
{
    return m_strings ? UtilsMOM::StringFromLayersString(mom_urilist_uri(m_strings, index)) : String();
}

// unused
String MOMStringCollection::namedItem( const String & name )
{
    return String();
}

String MOMStringCollection::join( const String & sep )
{
    if (!m_strings) return String();

    layers_string lsep = UtilsMOM::LayersStringFromString(sep);
    layers_string lstr = mom_urilist_create_string(m_strings, lsep);
    String str = UtilsMOM::StringFromLayersString(lstr);
    layers_string_dispose(lstr);
    layers_string_dispose(lsep);
    return str;
}

String MOMStringCollection::toString()
{
    if (!m_strings) return String();

    layers_string lstr = mom_urilist_create_csv(m_strings);
    String str = UtilsMOM::StringFromLayersString(lstr);
    layers_string_dispose(lstr);
    return str;
}


} // namespace WebCore

#endif // ENABLE(RMAI)
