/**
 * Copyright (C) 2015 Laverna project Authors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
define(["underscore"],function(e){"use strict";var t={pattern:/\[(X|\s|\_|\-)?\]\s(.*)/i,globPattern:/\[(X|\s|\_|\-)?\]\s(.*)/gi,init:function(e){e.core.ruler.push("task",t._taskReplace(e)),e.renderer.rules.task_tag=t._renderTask},toggle:function(e){var n=0;return e.content=e.content.replace(t.globPattern,function(t,c,a){return n++,n!==e.taskId?t:(c="x"===c||"X"===c?" ":"x","["+c+"] "+a)}),e.content},_taskReplace:function(n){var c=n.utils.arrayReplaceAt;return function(n){var a,s=0;e.each(n.tokens,function(r){return"inline"!==r.type?(a=r.content.match(t.globPattern),void(a&&(s+=a.length))):void e.each(r.children,function(e,a){"text"===e.type&&t.pattern.test(e.content)&&(s++,r.children=c(r.children,a,t._replaceToken(e,n.Token,s)))})})}},_replaceToken:function(e,n,c){var a,s=e.content.match(t.pattern),r=s[1],o=s[2],l="X"===r||"x"===r;return a=new n("task_tag","",0),a.meta={label:o,checked:l,id:c},a.children=[],[a]},_renderTask:function(e,t,n,c){var a=e[t].meta;return c&&(c.tasks=c.tasks||[],c.tasks.push(a.label),a.checked?c.taskCompleted=(c.taskCompleted||0)+1:c.taskCompleted=c.taskCompleted||0),'<label class="task task--checkbox"><input data-task="'+a.id+'" type="checkbox"'+(a.checked?'checked="checked"':"")+' class="checkbox--input" /><svg class="checkbox--svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path class="checkbox--path" d="M16.667,62.167c3.109,5.55,7.217,10.591,10.926,15.75 c2.614,3.636,5.149,7.519,8.161,10.853c-0.046-0.051,1.959,2.414,2.692,2.343c0.895-0.088,6.958-8.511,6.014-7.3 c5.997-7.695,11.68-15.463,16.931-23.696c6.393-10.025,12.235-20.373,18.104-30.707C82.004,24.988,84.802,20.601,87,16"></path></svg><span class="checkbox--text">'+a.label+"</span></label>"}};return t});