package transport

import (
	

	

	
)

// Protocol is MTProto transport protocol.
//
// See https://core.telegram.org/mtproto/mtproto-transports
type Protocol struct {
	codec func() Codec
}

// NewProtocol creates new transport protocol using user Codec constructor.
//
// See https://core.telegram.org/mtproto/mtproto-transports
func ( func() Codec) Protocol {
	return Protocol{
		codec: ,
	}
}

// Telegram transport protocols.
//
// See https://core.telegram.org/mtproto/mtproto-transports
var (
	// Abridged is abridged transport protocol.
	//
	// See https://core.telegram.org/mtproto/mtproto-transports#abridged
	Abridged = NewProtocol(func() Codec { return codec.Abridged{} })

	// Intermediate is intermediate transport protocol.
	//
	// See https://core.telegram.org/mtproto/mtproto-transports#intermediate
	Intermediate = NewProtocol(func() Codec { return codec.Intermediate{} })

	// PaddedIntermediate is padded intermediate transport protocol.
	//
	// See https://core.telegram.org/mtproto/mtproto-transports#padded-intermediate
	PaddedIntermediate = NewProtocol(func() Codec { return codec.PaddedIntermediate{} })

	// Full is full transport protocol.
	//
	// See https://core.telegram.org/mtproto/mtproto-transports#full
	Full = NewProtocol(func() Codec { return &codec.Full{} })
)

// Codec creates new codec using protocol settings.
func ( Protocol) () Codec {
	return .codec()
}

// CodecNoHeader is Codec without header.
func ( Protocol) () Codec {
	return codec.NoHeader{Codec: .codec()}
}

// Handshake inits given net.Conn as MTProto connection.
func ( Protocol) ( net.Conn) (Conn, error) {
	 := .codec()
	if  := .WriteHeader();  != nil {
		return nil, errors.Wrap(, "write header")
	}

	return &connection{
		conn:  ,
		codec: ,
	}, nil
}

// Pipe creates an in-memory MTProto connection.
func ( Protocol) () (,  Conn) {
	,  := net.Pipe()

	return &connection{
			conn:  ,
			codec: .codec(),
		}, &connection{
			conn:  ,
			codec: .codec(),
		}
}