// Compiled by ClojureScript 1.10.520 {}
goog.provide('weathergen.util');
goog.require('cljs.core');
goog.require('clojure.set');
goog.require('clojure.string');
/**
 * Convert a string to a long. Returns nil when it can't be parsed.
 */
weathergen.util.str__GT_long = (function weathergen$util$str__GT_long(s){
var n = (new Number(s)).valueOf();
if(cljs.core.int_QMARK_.call(null,n)){
return cljs.core.long$.call(null,n);
} else {
return null;
}
});
/**
 * Convert a string to a floating point number. Returns nil when it can't be parsed.
 */
weathergen.util.str__GT_float = (function weathergen$util$str__GT_float(s){
var n = (new Number(s)).valueOf();
if(cljs.core.truth_(isNaN(n))){
return null;
} else {
return n;
}
});
/**
 * Concatenates `colls` into a vector. Order of elements is preserved.
 */
weathergen.util.concatv = (function weathergen$util$concatv(var_args){
var args__4736__auto__ = [];
var len__4730__auto___24360 = arguments.length;
var i__4731__auto___24361 = (0);
while(true){
if((i__4731__auto___24361 < len__4730__auto___24360)){
args__4736__auto__.push((arguments[i__4731__auto___24361]));

var G__24362 = (i__4731__auto___24361 + (1));
i__4731__auto___24361 = G__24362;
continue;
} else {
}
break;
}

var argseq__4737__auto__ = ((((0) < args__4736__auto__.length))?(new cljs.core.IndexedSeq(args__4736__auto__.slice((0)),(0),null)):null);
return weathergen.util.concatv.cljs$core$IFn$_invoke$arity$variadic(argseq__4737__auto__);
});

weathergen.util.concatv.cljs$core$IFn$_invoke$arity$variadic = (function (colls){
return cljs.core.reduce.call(null,cljs.core.into,cljs.core.PersistentVector.EMPTY,colls);
});

weathergen.util.concatv.cljs$lang$maxFixedArity = (0);

/** @this {Function} */
weathergen.util.concatv.cljs$lang$applyTo = (function (seq24359){
var self__4718__auto__ = this;
return self__4718__auto__.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq24359));
});

/**
 * Filters `coll` for an x where `(f x)` is equal to `val`
 */
weathergen.util.filter_EQ_ = (function weathergen$util$filter_EQ_(f,val,coll){
return cljs.core.filter.call(null,(function (p1__24363_SHARP_){
return cljs.core._EQ_.call(null,f.call(null,p1__24363_SHARP_),val);
}),coll);
});
/**
 * Returns the first element of a sequence, or nil if it's empty, or
 *   throws an exception if there is more than one element.
 */
weathergen.util.only = (function weathergen$util$only(coll){
if(cljs.core.empty_QMARK_.call(null,coll)){
return null;
} else {
if(cljs.core.empty_QMARK_.call(null,cljs.core.rest.call(null,coll))){
return cljs.core.first.call(null,coll);
} else {
throw cljs.core.ex_info.call(null,"Sequence had more than one item",new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"reason","reason",-2070751759),new cljs.core.Keyword(null,"not-singular","not-singular",1360522305),new cljs.core.Keyword(null,"coll","coll",1647737163),coll], null));

}
}
});
/**
 * Like take-while, but also includes the first item where `pred`
 *   returns true.
 */
weathergen.util.take_until = (function weathergen$util$take_until(pred,coll){
if(cljs.core.empty_QMARK_.call(null,coll)){
return coll;
} else {
if(cljs.core.truth_(pred.call(null,cljs.core.first.call(null,coll)))){
return cljs.core.take.call(null,(1),coll);
} else {
var vec__24364 = cljs.core.partition_by.call(null,pred,coll);
var head = cljs.core.nth.call(null,vec__24364,(0),null);
var next = cljs.core.nth.call(null,vec__24364,(1),null);
return cljs.core.concat.call(null,head,cljs.core.take.call(null,(1),next));

}
}
});
/**
 * If `x` is a member of `s`, remove it. Otherwise, add it.
 */
weathergen.util.toggle_set_membership = (function weathergen$util$toggle_set_membership(s,x){
var op = ((cljs.core.contains_QMARK_.call(null,s,x))?cljs.core.disj:cljs.core.conj);
return op.call(null,s,x);
});
/**
 * Returns true if all of the bits set in `mask` are also set in `x`.
 */
weathergen.util.has_flag_QMARK_ = (function weathergen$util$has_flag_QMARK_(x,mask){
return cljs.core._EQ_.call(null,(x & mask),mask);
});
/**
 * Given a collection of strings, concatenate them together inserting
 *   commas and the word "and" as appropirate.
 */
weathergen.util.format_list = (function weathergen$util$format_list(var_args){
var G__24368 = arguments.length;
switch (G__24368) {
case 1:
return weathergen.util.format_list.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));

break;
case 2:
return weathergen.util.format_list.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
throw (new Error(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.length)].join('')));

}
});

weathergen.util.format_list.cljs$core$IFn$_invoke$arity$1 = (function (items){
return weathergen.util.format_list.call(null,new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"conjunction","conjunction",-917061292),"and",new cljs.core.Keyword(null,"separator","separator",-1628749125),","], null),items);
});

weathergen.util.format_list.cljs$core$IFn$_invoke$arity$2 = (function (p__24369,items){
var map__24370 = p__24369;
var map__24370__$1 = (((((!((map__24370 == null))))?(((((map__24370.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__24370.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__24370):map__24370);
var opts = map__24370__$1;
var separator = cljs.core.get.call(null,map__24370__$1,new cljs.core.Keyword(null,"separator","separator",-1628749125));
var conjunction = cljs.core.get.call(null,map__24370__$1,new cljs.core.Keyword(null,"conjunction","conjunction",-917061292));
var n = cljs.core.count.call(null,items);
var separator__$1 = (function (){var or__4131__auto__ = separator;
if(cljs.core.truth_(or__4131__auto__)){
return or__4131__auto__;
} else {
return ",";
}
})();
if((cljs.core.count.call(null,items) < (2))){
return cljs.core.first.call(null,items);
} else {
return [cljs.core.str.cljs$core$IFn$_invoke$arity$1(clojure.string.join.call(null,[cljs.core.str.cljs$core$IFn$_invoke$arity$1(separator__$1)," "].join(''),cljs.core.butlast.call(null,items))),cljs.core.str.cljs$core$IFn$_invoke$arity$1(((((2) < cljs.core.count.call(null,items)))?separator__$1:null))," ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(conjunction)," ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(cljs.core.last.call(null,items))].join('');
}
});

weathergen.util.format_list.cljs$lang$maxFixedArity = 2;

weathergen.util.enquote = (function weathergen$util$enquote(v){
return ["\"",cljs.core.str.cljs$core$IFn$_invoke$arity$1(clojure.string.replace.call(null,cljs.core.str.cljs$core$IFn$_invoke$arity$1(v),"\"","''")),"\""].join('');
});
/**
 * Turn x into a vector if it isn't already.
 */
weathergen.util.vectorize = (function weathergen$util$vectorize(x){
if(cljs.core.vector_QMARK_.call(null,x)){
return x;
} else {
return new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [x], null);
}
});
/**
 * Given a nested map, flatten it so all keys are top-level.
 */
weathergen.util.flatten_map = (function weathergen$util$flatten_map(m){
return cljs.core.reduce_kv.call(null,(function (m__$1,k,v){
if((!(cljs.core.map_QMARK_.call(null,v)))){
return cljs.core.assoc.call(null,m__$1,weathergen.util.vectorize.call(null,k),v);
} else {
return cljs.core.merge.call(null,m__$1,weathergen.util.flatten_map.call(null,cljs.core.into.call(null,cljs.core.PersistentArrayMap.EMPTY,cljs.core.map.call(null,(function (p__24373){
var vec__24374 = p__24373;
var ki = cljs.core.nth.call(null,vec__24374,(0),null);
var vi = cljs.core.nth.call(null,vec__24374,(1),null);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [cljs.core.conj.call(null,weathergen.util.vectorize.call(null,k),ki),vi], null);
}),v))));
}
}),cljs.core.PersistentArrayMap.EMPTY,m);
});
/**
 * Given a collection of maps, return out a CSV string. Options can
 *   include :initial-columns, an ordering of columns to appear first. Remaining
 *   columns will be lexically sorted.
 */
weathergen.util.csv_ize = (function weathergen$util$csv_ize(var_args){
var G__24378 = arguments.length;
switch (G__24378) {
case 1:
return weathergen.util.csv_ize.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));

break;
case 2:
return weathergen.util.csv_ize.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
throw (new Error(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.length)].join('')));

}
});

weathergen.util.csv_ize.cljs$core$IFn$_invoke$arity$1 = (function (maps){
return weathergen.util.csv_ize.call(null,maps,cljs.core.PersistentArrayMap.EMPTY);
});

weathergen.util.csv_ize.cljs$core$IFn$_invoke$arity$2 = (function (maps,p__24379){
var map__24380 = p__24379;
var map__24380__$1 = (((((!((map__24380 == null))))?(((((map__24380.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__24380.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.call(null,cljs.core.hash_map,map__24380):map__24380);
var options = map__24380__$1;
var initial_columns = cljs.core.get.call(null,map__24380__$1,new cljs.core.Keyword(null,"initial-columns","initial-columns",1267275349),cljs.core.PersistentVector.EMPTY);
var key_remap = cljs.core.get.call(null,map__24380__$1,new cljs.core.Keyword(null,"key-remap","key-remap",-767728308),cljs.core.identity);
var flattened_initial = cljs.core.mapv.call(null,weathergen.util.vectorize,initial_columns);
var flattened = cljs.core.mapv.call(null,weathergen.util.flatten_map,maps);
var cols = (function (){var _QMARK_ = flattened;
var _QMARK___$1 = cljs.core.mapcat.call(null,cljs.core.keys,_QMARK_);
var _QMARK___$2 = cljs.core.set.call(null,_QMARK___$1);
var _QMARK___$3 = clojure.set.difference.call(null,_QMARK___$2,cljs.core.set.call(null,flattened_initial));
var _QMARK___$4 = cljs.core.sort.call(null,_QMARK___$3);
var _QMARK___$5 = cljs.core.vec.call(null,_QMARK___$4);
return cljs.core.into.call(null,flattened_initial,_QMARK___$5);
})();
var get_vals = ((function (flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap){
return (function (item){
var iter__4523__auto__ = ((function (flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap){
return (function weathergen$util$iter__24382(s__24383){
return (new cljs.core.LazySeq(null,((function (flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap){
return (function (){
var s__24383__$1 = s__24383;
while(true){
var temp__5720__auto__ = cljs.core.seq.call(null,s__24383__$1);
if(temp__5720__auto__){
var s__24383__$2 = temp__5720__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,s__24383__$2)){
var c__4521__auto__ = cljs.core.chunk_first.call(null,s__24383__$2);
var size__4522__auto__ = cljs.core.count.call(null,c__4521__auto__);
var b__24385 = cljs.core.chunk_buffer.call(null,size__4522__auto__);
if((function (){var i__24384 = (0);
while(true){
if((i__24384 < size__4522__auto__)){
var col = cljs.core._nth.call(null,c__4521__auto__,i__24384);
cljs.core.chunk_append.call(null,b__24385,cljs.core.get.call(null,item,col));

var G__24387 = (i__24384 + (1));
i__24384 = G__24387;
continue;
} else {
return true;
}
break;
}
})()){
return cljs.core.chunk_cons.call(null,cljs.core.chunk.call(null,b__24385),weathergen$util$iter__24382.call(null,cljs.core.chunk_rest.call(null,s__24383__$2)));
} else {
return cljs.core.chunk_cons.call(null,cljs.core.chunk.call(null,b__24385),null);
}
} else {
var col = cljs.core.first.call(null,s__24383__$2);
return cljs.core.cons.call(null,cljs.core.get.call(null,item,col),weathergen$util$iter__24382.call(null,cljs.core.rest.call(null,s__24383__$2)));
}
} else {
return null;
}
break;
}
});})(flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap))
,null,null));
});})(flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap))
;
return iter__4523__auto__.call(null,cols);
});})(flattened_initial,flattened,cols,map__24380,map__24380__$1,options,initial_columns,key_remap))
;
var header = cljs.core.apply.call(null,cljs.core.str,"index,",cljs.core.interpose.call(null,",",cljs.core.map.call(null,cljs.core.str,cljs.core.map.call(null,key_remap,cols))));
var row = ((function (flattened_initial,flattened,cols,get_vals,header,map__24380,map__24380__$1,options,initial_columns,key_remap){
return (function (index,item){
return cljs.core.apply.call(null,cljs.core.str,index,",",cljs.core.interpose.call(null,",",cljs.core.map.call(null,weathergen.util.enquote,get_vals.call(null,item))));
});})(flattened_initial,flattened,cols,get_vals,header,map__24380,map__24380__$1,options,initial_columns,key_remap))
;
return cljs.core.apply.call(null,cljs.core.str,cljs.core.interpose.call(null,"\n",cljs.core.into.call(null,new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [header], null),cljs.core.map_indexed.call(null,row,flattened))));
});

weathergen.util.csv_ize.cljs$lang$maxFixedArity = 2;

/**
 * Like merge-with, but merges maps recursively, applying the given fn
 *   only when there's a non-map at a particular level.
 *   (deep-merge-with + {:a {:b {:c 1 :d {:x 1 :y 2}} :e 3} :f 4}
 *                   {:a {:b {:c 2 :d {:z 9} :z 3} :e 100}})
 *   -> {:a {:b {:z 3, :c 3, :d {:z 9, :x 1, :y 2}}, :e 103}, :f 4}
 */
weathergen.util.deep_merge_with = (function weathergen$util$deep_merge_with(var_args){
var args__4736__auto__ = [];
var len__4730__auto___24390 = arguments.length;
var i__4731__auto___24391 = (0);
while(true){
if((i__4731__auto___24391 < len__4730__auto___24390)){
args__4736__auto__.push((arguments[i__4731__auto___24391]));

var G__24392 = (i__4731__auto___24391 + (1));
i__4731__auto___24391 = G__24392;
continue;
} else {
}
break;
}

var argseq__4737__auto__ = ((((1) < args__4736__auto__.length))?(new cljs.core.IndexedSeq(args__4736__auto__.slice((1)),(0),null)):null);
return weathergen.util.deep_merge_with.cljs$core$IFn$_invoke$arity$variadic((arguments[(0)]),argseq__4737__auto__);
});

weathergen.util.deep_merge_with.cljs$core$IFn$_invoke$arity$variadic = (function (f,maps){
return cljs.core.apply.call(null,(function() { 
var weathergen$util$m__delegate = function (maps__$1){
if(cljs.core.every_QMARK_.call(null,cljs.core.map_QMARK_,maps__$1)){
return cljs.core.apply.call(null,cljs.core.merge_with,weathergen$util$m,maps__$1);
} else {
return cljs.core.apply.call(null,f,maps__$1);
}
};
var weathergen$util$m = function (var_args){
var maps__$1 = null;
if (arguments.length > 0) {
var G__24393__i = 0, G__24393__a = new Array(arguments.length -  0);
while (G__24393__i < G__24393__a.length) {G__24393__a[G__24393__i] = arguments[G__24393__i + 0]; ++G__24393__i;}
  maps__$1 = new cljs.core.IndexedSeq(G__24393__a,0,null);
} 
return weathergen$util$m__delegate.call(this,maps__$1);};
weathergen$util$m.cljs$lang$maxFixedArity = 0;
weathergen$util$m.cljs$lang$applyTo = (function (arglist__24394){
var maps__$1 = cljs.core.seq(arglist__24394);
return weathergen$util$m__delegate(maps__$1);
});
weathergen$util$m.cljs$core$IFn$_invoke$arity$variadic = weathergen$util$m__delegate;
return weathergen$util$m;
})()
,maps);
});

weathergen.util.deep_merge_with.cljs$lang$maxFixedArity = (1);

/** @this {Function} */
weathergen.util.deep_merge_with.cljs$lang$applyTo = (function (seq24388){
var G__24389 = cljs.core.first.call(null,seq24388);
var seq24388__$1 = cljs.core.next.call(null,seq24388);
var self__4717__auto__ = this;
return self__4717__auto__.cljs$core$IFn$_invoke$arity$variadic(G__24389,seq24388__$1);
});

