// Compiled by ClojureScript 1.10.520 {}
goog.provide('octet.spec.reference');
goog.require('cljs.core');
goog.require('octet.buffer');
goog.require('octet.spec');
goog.require('octet.spec.basic');
goog.require('octet.spec.string');
octet.spec.reference.ref_size = (function octet$spec$reference$ref_size(type,data){
if((((!((type == null))))?((((false) || ((cljs.core.PROTOCOL_SENTINEL === type.octet$spec$ISpecDynamicSize$))))?true:(((!type.cljs$lang$protocol_mask$partition$))?cljs.core.native_satisfies_QMARK_.call(null,octet.spec.ISpecDynamicSize,type):false)):cljs.core.native_satisfies_QMARK_.call(null,octet.spec.ISpecDynamicSize,type))){
return octet.spec.size_STAR_.call(null,type,data);
} else {
if((((!((type == null))))?((((false) || ((cljs.core.PROTOCOL_SENTINEL === type.octet$spec$ISpecSize$))))?true:(((!type.cljs$lang$protocol_mask$partition$))?cljs.core.native_satisfies_QMARK_.call(null,octet.spec.ISpecSize,type):false)):cljs.core.native_satisfies_QMARK_.call(null,octet.spec.ISpecSize,type))){
return octet.spec.size.call(null,type);
} else {
throw cljs.core.ex_info.call(null,"Unexpected type",new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"type","type",1174270348),type], null));

}
}
});
/**
 * to make it terser to handle both maps (assoc spec) and seq/vector
 *   (indexed spec), we coerce the seq/vector types to maps where the
 *   keys are indexes
 */
octet.spec.reference.coerce_types = (function octet$spec$reference$coerce_types(types){
if(cljs.core.map_QMARK_.call(null,types)){
return types;
} else {
if(((cljs.core.seq_QMARK_.call(null,types)) || (cljs.core.vector_QMARK_.call(null,types)))){
return cljs.core.apply.call(null,cljs.core.array_map,cljs.core.interleave.call(null,cljs.core.range.call(null),types));
} else {
throw cljs.core.ex_info.call(null,"invalid type structure, not map, seq or vector",new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"type-structure","type-structure",-185390547),types], null));

}
}
});
/**
 * for ref-xxxx specs, calculate the byte offset of the
 *   spec containing the length, i.e. (spec :a (int16) :b (int16) c: (ref-string* :b))
 *   would cause this method to be called with :b (since the ref-string has a :b
 *   reference as its length) and should then return 2 as the second int16 is at byte offset 2
 */
octet.spec.reference.ref_len_offset = (function octet$spec$reference$ref_len_offset(ref_kw_or_index,types,data){
return cljs.core.reduce_kv.call(null,(function (acc,kw_or_index,type){
if(cljs.core._EQ_.call(null,ref_kw_or_index,kw_or_index)){
return cljs.core.reduced.call(null,acc);
} else {
return (acc + octet.spec.reference.ref_size.call(null,type,cljs.core.get.call(null,data,kw_or_index)));
}
}),(0),types);
});
octet.spec.reference.ref_write_length = (function octet$spec$reference$ref_write_length(type,buff,offset,length){
if((octet.spec.basic.int16 === type)){
return octet.buffer.write_short.call(null,buff,offset,length);
} else {
if((octet.spec.basic.int32 === type)){
return octet.buffer.write_int.call(null,buff,offset,length);
} else {
if((octet.spec.basic.int64 === type)){
return octet.buffer.write_long.call(null,buff,offset,length);
} else {
if((octet.spec.basic.int64 === type)){
return octet.buffer.write_long.call(null,buff,offset,length);
} else {
if((octet.spec.basic.uint16 === type)){
return octet.buffer.write_ushort.call(null,buff,offset,length);
} else {
if((octet.spec.basic.uint32 === type)){
return octet.buffer.write_uint.call(null,buff,offset,length);
} else {
if((octet.spec.basic.uint64 === type)){
return octet.buffer.write_ulong.call(null,buff,offset,length);
} else {
throw cljs.core.ex_info.call(null,"Invalid reference type: should be int16, int32, int64 and unsigned variants",cljs.core.PersistentArrayMap.EMPTY);

}
}
}
}
}
}
}
});
/**
 * write a ref spec, will also write the length to the length spec
 */
octet.spec.reference.ref_write = (function octet$spec$reference$ref_write(ref_kw_or_index,buff,pos,value,types,data){
var input = ((typeof value === 'string')?octet.spec.string.string__GT_bytes.call(null,value):value);
var length = cljs.core.count.call(null,input);
var types__$1 = octet.spec.reference.coerce_types.call(null,types);
var len_offset = octet.spec.reference.ref_len_offset.call(null,ref_kw_or_index,types__$1,data);
var len_type = cljs.core.get.call(null,types__$1,ref_kw_or_index);
octet.spec.reference.ref_write_length.call(null,len_type,buff,len_offset,length);

octet.buffer.write_bytes.call(null,buff,pos,length,input);

return length;
});
/**
 * read ref spec, will read the length from the length spec
 */
octet.spec.reference.ref_read = (function octet$spec$reference$ref_read(ref_kw_or_index,buff,pos,parent){
var datasize = ((cljs.core.map_QMARK_.call(null,parent))?ref_kw_or_index.call(null,parent):((((cljs.core.seq_QMARK_.call(null,parent)) || (cljs.core.vector_QMARK_.call(null,parent))))?cljs.core.get.call(null,parent,ref_kw_or_index):(function(){throw cljs.core.ex_info.call(null,["bad ref-string/ref-bytes length reference  - ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(ref_kw_or_index)].join(''),new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"length-kw","length-kw",-865148983),ref_kw_or_index,new cljs.core.Keyword(null,"data-read","data-read",-960788784),parent], null))})()
));
var data = octet.buffer.read_bytes.call(null,buff,pos,datasize);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [datasize,data], null);
});
/**
 * create a dynamic length byte array where the length of the byte array is stored in another
 *   spec within the containing indexed spec or associative spec. Example usages:
 *   (spec (int16) (int16) (ref-bytes* 1))
 *   (spec :a (int16) :b (int32) (ref-bytes* :b))
 *   where the first example would store the length of the byte array in the second int16 and
 *   the second example would store the length of the byte array in the int32 at key :b.
 */
octet.spec.reference.ref_bytes = (function octet$spec$reference$ref_bytes(ref_kw_or_index){
if((typeof octet !== 'undefined') && (typeof octet.spec !== 'undefined') && (typeof octet.spec.reference !== 'undefined') && (typeof octet.spec.reference.t_octet$spec$reference27533 !== 'undefined')){
} else {

/**
* @constructor
 * @implements {cljs.core.IFn}
 * @implements {octet.spec.ISpecDynamicSize}
 * @implements {octet.spec.ISpecWithRef}
 * @implements {cljs.core.IMeta}
 * @implements {cljs.core.IWithMeta}
*/
octet.spec.reference.t_octet$spec$reference27533 = (function (ref_kw_or_index,meta27534){
this.ref_kw_or_index = ref_kw_or_index;
this.meta27534 = meta27534;
this.cljs$lang$protocol_mask$partition0$ = 393217;
this.cljs$lang$protocol_mask$partition1$ = 0;
});
octet.spec.reference.t_octet$spec$reference27533.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_27535,meta27534__$1){
var self__ = this;
var _27535__$1 = this;
return (new octet.spec.reference.t_octet$spec$reference27533(self__.ref_kw_or_index,meta27534__$1));
});

octet.spec.reference.t_octet$spec$reference27533.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_27535){
var self__ = this;
var _27535__$1 = this;
return self__.meta27534;
});

octet.spec.reference.t_octet$spec$reference27533.prototype.call = (function (self__){
var self__ = this;
var self____$1 = this;
var s = self____$1;
return s;
});

octet.spec.reference.t_octet$spec$reference27533.prototype.apply = (function (self__,args27536){
var self__ = this;
var self____$1 = this;
return self____$1.call.apply(self____$1,[self____$1].concat(cljs.core.aclone.call(null,args27536)));
});

octet.spec.reference.t_octet$spec$reference27533.prototype.cljs$core$IFn$_invoke$arity$0 = (function (){
var self__ = this;
var s = this;
return s;
});

octet.spec.reference.t_octet$spec$reference27533.prototype.octet$spec$ISpecDynamicSize$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.reference.t_octet$spec$reference27533.prototype.octet$spec$ISpecDynamicSize$size_STAR_$arity$2 = (function (_,data){
var self__ = this;
var ___$1 = this;
return cljs.core.count.call(null,data);
});

octet.spec.reference.t_octet$spec$reference27533.prototype.octet$spec$ISpecWithRef$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.reference.t_octet$spec$reference27533.prototype.octet$spec$ISpecWithRef$read_STAR_$arity$4 = (function (_,buff,pos,parent){
var self__ = this;
var ___$1 = this;
return octet.spec.reference.ref_read.call(null,self__.ref_kw_or_index,buff,pos,parent);
});

octet.spec.reference.t_octet$spec$reference27533.prototype.octet$spec$ISpecWithRef$write_STAR_$arity$6 = (function (_,buff,pos,value,types,data){
var self__ = this;
var ___$1 = this;
return octet.spec.reference.ref_write.call(null,self__.ref_kw_or_index,buff,pos,value,types,data);
});

octet.spec.reference.t_octet$spec$reference27533.getBasis = (function (){
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"ref-kw-or-index","ref-kw-or-index",184543845,null),new cljs.core.Symbol(null,"meta27534","meta27534",-288396127,null)], null);
});

octet.spec.reference.t_octet$spec$reference27533.cljs$lang$type = true;

octet.spec.reference.t_octet$spec$reference27533.cljs$lang$ctorStr = "octet.spec.reference/t_octet$spec$reference27533";

octet.spec.reference.t_octet$spec$reference27533.cljs$lang$ctorPrWriter = (function (this__4374__auto__,writer__4375__auto__,opt__4376__auto__){
return cljs.core._write.call(null,writer__4375__auto__,"octet.spec.reference/t_octet$spec$reference27533");
});

/**
 * Positional factory function for octet.spec.reference/t_octet$spec$reference27533.
 */
octet.spec.reference.__GT_t_octet$spec$reference27533 = (function octet$spec$reference$ref_bytes_$___GT_t_octet$spec$reference27533(ref_kw_or_index__$1,meta27534){
return (new octet.spec.reference.t_octet$spec$reference27533(ref_kw_or_index__$1,meta27534));
});

}

return (new octet.spec.reference.t_octet$spec$reference27533(ref_kw_or_index,cljs.core.PersistentArrayMap.EMPTY));
});
/**
 * create a dynamic length string  where the length of the string is stored in another
 *   spec within the containing indexed spec or associative spec. Example usages:
 *   (spec (int16) (int16) (ref-string* 1))
 *   (spec :a (int16) :b (int32) (ref-string* :b))
 *   where the first example would store the length of the string in the second int16 and
 *   the second example would store the length of the string in the int32 at key :b.
 */
octet.spec.reference.ref_string = (function octet$spec$reference$ref_string(ref_kw_or_index){
if((typeof octet !== 'undefined') && (typeof octet.spec !== 'undefined') && (typeof octet.spec.reference !== 'undefined') && (typeof octet.spec.reference.t_octet$spec$reference27537 !== 'undefined')){
} else {

/**
* @constructor
 * @implements {cljs.core.IFn}
 * @implements {octet.spec.ISpecDynamicSize}
 * @implements {octet.spec.ISpecWithRef}
 * @implements {cljs.core.IMeta}
 * @implements {cljs.core.IWithMeta}
*/
octet.spec.reference.t_octet$spec$reference27537 = (function (ref_kw_or_index,meta27538){
this.ref_kw_or_index = ref_kw_or_index;
this.meta27538 = meta27538;
this.cljs$lang$protocol_mask$partition0$ = 393217;
this.cljs$lang$protocol_mask$partition1$ = 0;
});
octet.spec.reference.t_octet$spec$reference27537.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_27539,meta27538__$1){
var self__ = this;
var _27539__$1 = this;
return (new octet.spec.reference.t_octet$spec$reference27537(self__.ref_kw_or_index,meta27538__$1));
});

octet.spec.reference.t_octet$spec$reference27537.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_27539){
var self__ = this;
var _27539__$1 = this;
return self__.meta27538;
});

octet.spec.reference.t_octet$spec$reference27537.prototype.call = (function (self__){
var self__ = this;
var self____$1 = this;
var s = self____$1;
return s;
});

octet.spec.reference.t_octet$spec$reference27537.prototype.apply = (function (self__,args27540){
var self__ = this;
var self____$1 = this;
return self____$1.call.apply(self____$1,[self____$1].concat(cljs.core.aclone.call(null,args27540)));
});

octet.spec.reference.t_octet$spec$reference27537.prototype.cljs$core$IFn$_invoke$arity$0 = (function (){
var self__ = this;
var s = this;
return s;
});

octet.spec.reference.t_octet$spec$reference27537.prototype.octet$spec$ISpecDynamicSize$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.reference.t_octet$spec$reference27537.prototype.octet$spec$ISpecDynamicSize$size_STAR_$arity$2 = (function (_,data){
var self__ = this;
var ___$1 = this;
var data__$1 = octet.spec.string.string__GT_bytes.call(null,data);
return cljs.core.count.call(null,data__$1);
});

octet.spec.reference.t_octet$spec$reference27537.prototype.octet$spec$ISpecWithRef$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.reference.t_octet$spec$reference27537.prototype.octet$spec$ISpecWithRef$read_STAR_$arity$4 = (function (_,buff,pos,parent){
var self__ = this;
var ___$1 = this;
var vec__27541 = octet.spec.reference.ref_read.call(null,self__.ref_kw_or_index,buff,pos,parent);
var datasize = cljs.core.nth.call(null,vec__27541,(0),null);
var bytes = cljs.core.nth.call(null,vec__27541,(1),null);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [datasize,octet.spec.string.bytes__GT_string.call(null,bytes,datasize)], null);
});

octet.spec.reference.t_octet$spec$reference27537.prototype.octet$spec$ISpecWithRef$write_STAR_$arity$6 = (function (_,buff,pos,value,types,data){
var self__ = this;
var ___$1 = this;
return octet.spec.reference.ref_write.call(null,self__.ref_kw_or_index,buff,pos,value,types,data);
});

octet.spec.reference.t_octet$spec$reference27537.getBasis = (function (){
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"ref-kw-or-index","ref-kw-or-index",184543845,null),new cljs.core.Symbol(null,"meta27538","meta27538",1913477731,null)], null);
});

octet.spec.reference.t_octet$spec$reference27537.cljs$lang$type = true;

octet.spec.reference.t_octet$spec$reference27537.cljs$lang$ctorStr = "octet.spec.reference/t_octet$spec$reference27537";

octet.spec.reference.t_octet$spec$reference27537.cljs$lang$ctorPrWriter = (function (this__4374__auto__,writer__4375__auto__,opt__4376__auto__){
return cljs.core._write.call(null,writer__4375__auto__,"octet.spec.reference/t_octet$spec$reference27537");
});

/**
 * Positional factory function for octet.spec.reference/t_octet$spec$reference27537.
 */
octet.spec.reference.__GT_t_octet$spec$reference27537 = (function octet$spec$reference$ref_string_$___GT_t_octet$spec$reference27537(ref_kw_or_index__$1,meta27538){
return (new octet.spec.reference.t_octet$spec$reference27537(ref_kw_or_index__$1,meta27538));
});

}

return (new octet.spec.reference.t_octet$spec$reference27537(ref_kw_or_index,cljs.core.PersistentArrayMap.EMPTY));
});
