package org.mercury_im.messenger.core.di.module;

import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.jivesoftware.smackx.ikey.util.canonicalization.ElementCanonicalizer;
import org.jivesoftware.smackx.ikey.util.canonicalization.XmlSecElementCanonicalizer;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class IkeyModule {

    @Singleton
    @Provides
    static Canonicalizer provideCanonicalizer() {
        if (!Init.isInitialized()) {
            Init.init();
        }
        try {
            return Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
        } catch (InvalidCanonicalizerException e) {
            throw new AssertionError(e);
        }
    }

    @Singleton
    @Provides
    static ElementCanonicalizer provideElementCanonicalizer(Canonicalizer canonicalizer) {
        return new XmlSecElementCanonicalizer(canonicalizer);
    }
}
