import "./cljs_env.js";
goog.provide('clojure_eval.sci');
var module$node_modules$$codemirror$view$dist$index_cjs=shadow.js.require("module$node_modules$$codemirror$view$dist$index_cjs", {});
if((typeof clojure_eval !== 'undefined') && (typeof clojure_eval.sci !== 'undefined') && (typeof clojure_eval.sci._BANG_viewer !== 'undefined')){
} else {
clojure_eval.sci._BANG_viewer = cljs.core.atom.cljs$core$IFn$_invoke$arity$1("");
}
if((typeof clojure_eval !== 'undefined') && (typeof clojure_eval.sci !== 'undefined') && (typeof clojure_eval.sci.last_result !== 'undefined')){
} else {
clojure_eval.sci.last_result = cljs.core.atom.cljs$core$IFn$_invoke$arity$1("");
}
if((typeof clojure_eval !== 'undefined') && (typeof clojure_eval.sci !== 'undefined') && (typeof clojure_eval.sci.context !== 'undefined')){
} else {
clojure_eval.sci.context = sci.core.init(new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"classes","classes",2037804510),new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Symbol(null,"js","js",-886355190,null),goog.global,new cljs.core.Keyword(null,"allow","allow",-1857325745),new cljs.core.Keyword(null,"all","all",892129742)], null)], null));
}
clojure_eval.sci.eval_string = (function clojure_eval$sci$eval_string(var_args){
var G__58968 = arguments.length;
switch (G__58968) {
case 1:
return clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));

break;
case 2:
return clojure_eval.sci.eval_string.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('')));

}
});

(clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$1 = (function (source){
return clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$2(clojure_eval.sci.context,source);
}));

(clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$2 = (function (ctx,source){
var temp__5757__auto__ = cljs.core.not_empty(clojure.string.trim(source));
if((temp__5757__auto__ == null)){
return null;
} else {
var code = temp__5757__auto__;
try{return new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"result","result",1415092211),sci.core.eval_string_STAR_(ctx,code)], null);
}catch (e58969){if((e58969 instanceof Error)){
var e = e58969;
return new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"error","error",-978969032),cljs.core.str.cljs$core$IFn$_invoke$arity$1(e.message)], null);
} else {
throw e58969;

}
}}
}));

(clojure_eval.sci.eval_string.cljs$lang$maxFixedArity = 2);

if((typeof clojure_eval !== 'undefined') && (typeof clojure_eval.sci !== 'undefined') && (typeof clojure_eval.sci.eval_tail !== 'undefined')){
} else {
clojure_eval.sci.eval_tail = cljs.core.atom.cljs$core$IFn$_invoke$arity$1(null);
}
clojure_eval.sci.update_editor_BANG_ = (function clojure_eval$sci$update_editor_BANG_(text,cursor_pos){
var end = cljs.core.count((function (){var G__58970 = cljs.core.deref(clojure_eval.sci._BANG_viewer);
var G__58970__$1 = (((G__58970 == null))?null:G__58970.state);
var G__58970__$2 = (((G__58970__$1 == null))?null:G__58970__$1.doc);
if((G__58970__$2 == null)){
return null;
} else {
return cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__58970__$2);
}
})());
return cljs.core.deref(clojure_eval.sci._BANG_viewer).dispatch(({"changes": ({"from": (0), "to": end, "insert": text}), "selection": ({"anchor": cursor_pos, "head": cursor_pos})}));
});
clojure_eval.sci.eval_at_cursor = (function clojure_eval$sci$eval_at_cursor(on_result,p__58971){
var map__58972 = p__58971;
var map__58972__$1 = (((((!((map__58972 == null))))?(((((map__58972.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58972.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58972):map__58972);
var state = (function (){var obj58974 = map__58972__$1;
var k58975 = "state";
if((function (){var obj58976 = obj58974;
return (((!((obj58976 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58975,obj58976)));
})()){
return (obj58974[k58975]);
} else {
return undefined;
}
})();
var cursor_pos_59001 = (function (){var G__58977 = cljs.core.deref(clojure_eval.sci._BANG_viewer);
var G__58977__$1 = (((G__58977 == null))?null:G__58977.state);
var G__58977__$2 = (((G__58977__$1 == null))?null:G__58977__$1.selection);
var G__58977__$3 = (((G__58977__$2 == null))?null:G__58977__$2.main);
if((G__58977__$3 == null)){
return null;
} else {
return G__58977__$3.head;
}
})();
var code_59002 = cljs.core.first(clojure.string.split.cljs$core$IFn$_invoke$arity$2(cljs.core.str.cljs$core$IFn$_invoke$arity$1((function (){var G__58979 = cljs.core.deref(clojure_eval.sci._BANG_viewer);
var G__58979__$1 = (((G__58979 == null))?null:G__58979.state);
var G__58979__$2 = (((G__58979__$1 == null))?null:G__58979__$1.doc);
if((G__58979__$2 == null)){
return null;
} else {
return cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__58979__$2);
}
})()),/ => /));
var G__58980_59003 = nextjournal.clojure_mode.extensions.eval_region.cursor_node_string(state);
var G__58980_59004__$1 = (((G__58980_59003 == null))?null:clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$1(G__58980_59003));
if((G__58980_59004__$1 == null)){
} else {
(on_result.cljs$core$IFn$_invoke$arity$1 ? on_result.cljs$core$IFn$_invoke$arity$1(G__58980_59004__$1) : on_result.call(null,G__58980_59004__$1));
}

clojure_eval.sci.update_editor_BANG_([cljs.core.subs.cljs$core$IFn$_invoke$arity$3(code_59002,(0),cursor_pos_59001),((cljs.core._EQ_.cljs$core$IFn$_invoke$arity$2("",new cljs.core.Keyword(null,"result","result",1415092211).cljs$core$IFn$_invoke$arity$1(cljs.core.deref(clojure_eval.sci.last_result))))?null:" => "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(new cljs.core.Keyword(null,"result","result",1415092211).cljs$core$IFn$_invoke$arity$1(cljs.core.deref(clojure_eval.sci.last_result))),cljs.core.str.cljs$core$IFn$_invoke$arity$1(cljs.core.reset_BANG_(clojure_eval.sci.eval_tail,cljs.core.subs.cljs$core$IFn$_invoke$arity$3(code_59002,cursor_pos_59001,cljs.core.count(code_59002))))].join(''),cursor_pos_59001);

cljs.core.deref(clojure_eval.sci._BANG_viewer).dispatch(({"selection": ({"anchor": cursor_pos_59001, "head": cursor_pos_59001})}));

return true;
});
clojure_eval.sci.eval_top_level = (function clojure_eval$sci$eval_top_level(on_result,p__58981){
var map__58982 = p__58981;
var map__58982__$1 = (((((!((map__58982 == null))))?(((((map__58982.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58982.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58982):map__58982);
var state = (function (){var obj58984 = map__58982__$1;
var k58985 = "state";
if((function (){var obj58986 = obj58984;
return (((!((obj58986 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58985,obj58986)));
})()){
return (obj58984[k58985]);
} else {
return undefined;
}
})();
var G__58987_59005 = nextjournal.clojure_mode.extensions.eval_region.top_level_string(state);
var G__58987_59006__$1 = (((G__58987_59005 == null))?null:clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$1(G__58987_59005));
if((G__58987_59006__$1 == null)){
} else {
(on_result.cljs$core$IFn$_invoke$arity$1 ? on_result.cljs$core$IFn$_invoke$arity$1(G__58987_59006__$1) : on_result.call(null,G__58987_59006__$1));
}

return true;
});
clojure_eval.sci.eval_cell = (function clojure_eval$sci$eval_cell(on_result,p__58988){
var map__58989 = p__58988;
var map__58989__$1 = (((((!((map__58989 == null))))?(((((map__58989.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58989.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58989):map__58989);
var state = (function (){var obj58991 = map__58989__$1;
var k58992 = "state";
if((function (){var obj58993 = obj58991;
return (((!((obj58993 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58992,obj58993)));
})()){
return (obj58991[k58992]);
} else {
return undefined;
}
})();
var G__58994_59007 = clojure_eval.sci.eval_string.cljs$core$IFn$_invoke$arity$1(["(do ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(state.doc)," )"].join(''));
(on_result.cljs$core$IFn$_invoke$arity$1 ? on_result.cljs$core$IFn$_invoke$arity$1(G__58994_59007) : on_result.call(null,G__58994_59007));

return true;
});
clojure_eval.sci.clear_eval = (function clojure_eval$sci$clear_eval(){
if(cljs.core.not_EQ_.cljs$core$IFn$_invoke$arity$2("",cljs.core.deref(clojure_eval.sci.last_result))){
cljs.core.reset_BANG_(clojure_eval.sci.last_result,"");

var code = [cljs.core.str.cljs$core$IFn$_invoke$arity$1(cljs.core.first(clojure.string.split.cljs$core$IFn$_invoke$arity$2(cljs.core.str.cljs$core$IFn$_invoke$arity$1((function (){var G__58998 = cljs.core.deref(clojure_eval.sci._BANG_viewer);
var G__58998__$1 = (((G__58998 == null))?null:G__58998.state);
var G__58998__$2 = (((G__58998__$1 == null))?null:G__58998__$1.doc);
if((G__58998__$2 == null)){
return null;
} else {
return cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__58998__$2);
}
})()),/ => /))),cljs.core.str.cljs$core$IFn$_invoke$arity$1(cljs.core.deref(clojure_eval.sci.eval_tail))].join('');
var cursor_pos = (function (){var G__58999 = cljs.core.deref(clojure_eval.sci._BANG_viewer);
var G__58999__$1 = (((G__58999 == null))?null:G__58999.state);
var G__58999__$2 = (((G__58999__$1 == null))?null:G__58999__$1.selection);
var G__58999__$3 = (((G__58999__$2 == null))?null:G__58999__$2.main);
if((G__58999__$3 == null)){
return null;
} else {
return G__58999__$3.head;
}
})();
return clojure_eval.sci.update_editor_BANG_(code,(function (){var x__4309__auto__ = cursor_pos;
var y__4310__auto__ = ((code).length);
return ((x__4309__auto__ < y__4310__auto__) ? x__4309__auto__ : y__4310__auto__);
})());
} else {
return null;
}
});
clojure_eval.sci.extension = (function clojure_eval$sci$extension(){
return module$node_modules$$codemirror$view$dist$index_cjs.keymap.of([({"key":"Alt-Enter","run":cljs.core.partial.cljs$core$IFn$_invoke$arity$2(clojure_eval.sci.eval_cell,(function (result){
return cljs.core.reset_BANG_(clojure_eval.sci.last_result,result);
}))}),({"key":"Mod-Enter","run":cljs.core.partial.cljs$core$IFn$_invoke$arity$2(clojure_eval.sci.eval_top_level,(function (result){
return cljs.core.reset_BANG_(clojure_eval.sci.last_result,result);
}))}),({"key":"Shift-Enter","run":cljs.core.partial.cljs$core$IFn$_invoke$arity$2(clojure_eval.sci.eval_at_cursor,(function (result){
return cljs.core.reset_BANG_(clojure_eval.sci.last_result,result);
}))}),({"key":"Escape","run":clojure_eval.sci.clear_eval}),({"key":"ArrowLeft","run":clojure_eval.sci.clear_eval}),({"key":"ArrowRight","run":clojure_eval.sci.clear_eval}),({"key":"ArrowUp","run":clojure_eval.sci.clear_eval}),({"key":"ArrowDown","run":clojure_eval.sci.clear_eval})]);
});

//# sourceMappingURL=clojure_eval.sci.js.map
