text: "use strict";var utils=require("./utils.js"),_=require("lodash"),slugger=require("uslug");module.exports.swigFunctions=function(e){var t=this;this.context={},this.data={},this.settings={},this.typeInfo={},this.paginate=!1,this.curPage=1,this.maxPage=-1,this.pageUrl="page-",this.paginationBaseUrl=null,this.cachedData={},this.CURRENT_URL="/";var n=function(e){if("string"==typeof e){var t=i(!0);e=_.find(t,function(t){return t.name.toLowerCase()==e.toLowerCase()||t.id.toLowerCase()==e.toLowerCase()})}if(!e)return"";if(e.slug)return"/"+e.slug+"/";var n=e.name?slugger(e.name).toLowerCase():"",r=e._type?e._type:"",o="";return o=r?"/"+r+"/"+n+"/":"/"+n+"/"};this.setData=function(e){t.cachedData={},t.data=e},this.setTypeInfo=function(e){t.typeInfo=e},this.setSettings=function(e){t.settings=e};var i=function(e){var n=[];for(var i in t.typeInfo)if(e||!t.typeInfo[i].oneOff){var r=i;t.typeInfo[i]&&t.typeInfo[i].customUrls&&t.typeInfo[i].customUrls.listUrl&&"#"!==t.typeInfo[i].customUrls.listUrl&&(r=t.typeInfo[i].customUrls.listUrl),n.push({slug:r,name:t.typeInfo[i].name,id:i})}return n},r=function(e,n,i){if(!e)return{};if(!n){if(Array.isArray(e)){if(!(e.length>0))return{};e=e[0]}var r=e.split(" ",2);if(2!==r.length)return{};e=r[0],n=r[1]}if(!t.typeInfo[e])return{};var o=null;if(o=t.typeInfo[e].oneOff?t.data[e]:t.data[e][n],!o)return{};if(!i&&!t.typeInfo[e].oneOff){if(!o.publish_date)return{};var s=Date.now(),c=Date.parse(o.publish_date);if(c>s+6e4)return{}}var f=[];if(t.typeInfo[e]&&t.typeInfo[e].controls&&t.typeInfo[e].controls.forEach(function(e){"relation"===e.controlType?f.push({ownerField:null,name:e.name,isSingle:e.meta?e.meta.isSingle:!1}):"grid"===e.controlType&&e.controls&&e.controls.forEach(function(t){"relation"===t.controlType&&f.push({ownerField:e.name,name:t.name,isSingle:t.meta?t.meta.isSingle:!1})})}),o=l(f,o),o._type=e,o._id=n,!o.slug){var u=a(o),m="";t.typeInfo[e]&&t.typeInfo[e].customUrls&&t.typeInfo[e].customUrls.listUrl?"#"!==t.typeInfo[e].customUrls.listUrl&&(m=t.typeInfo[e].customUrls.listUrl+"/"):m=e+"/",t.typeInfo[e]&&t.typeInfo[e].customUrls&&t.typeInfo[e].customUrls.individualUrl&&(m+=utils.parseCustomUrl(t.typeInfo[e].customUrls.individualUrl,o)+"/"),m+=u,o.slug=m}return o},o=function(e,t){if(!e)return[];var n=[];return e.forEach(function(e){var i=r(e,null,t);_.isEmpty(i)||n.push(i)}),n},s={},a=function(e){if(s[e._type]||(s[e._type]={}),e.slug)return s[e._type][e.slug]=!0,e.slug;for(var t=slugger(e.name).toLowerCase(),n=2;s[e._type][t];)t=slugger(e.name).toLowerCase()+"_"+n,n++;return s[e._type][t]=!0,t},l=function(e,t){var n=function(e,t){var n=e[t.name];t.isSingle?(Object.defineProperty(e,t.name,{enumerable:!0,configurable:!0,get:function(){return n?r(n):n}}),Object.defineProperty(e,"_"+t.name,{enumerable:!1,configurable:!0,get:function(){return n?r(n,null,!0):n}})):(Object.defineProperty(e,t.name,{enumerable:!0,configurable:!0,get:function(){return n?o(n):n}}),Object.defineProperty(e,"_"+t.name,{enumerable:!1,configurable:!0,get:function(){return n?o(n,!0):n}}))};return e.forEach(function(e){if(e.ownerField){var i=t[e.ownerField];if(!i)return;i.forEach(function(t){var i=Object.getOwnPropertyDescriptor(t,e.name);i&&i.get||n(t,e)})}else{var r=Object.getOwnPropertyDescriptor(t,e.name);if(r&&r.get)return;n(t,e)}}),t},c=function(){var e=[].slice.call(arguments,0);if(0===e.length)return[];var n=e[e.length-1],i=!1;if("boolean"==typeof n&&(i=n,e.pop()),t.cachedData[e.join(",")+","+i])return t.cachedData[e.join(",")+","+i];s={};var r=[];return e.forEach(function(e){var n=t.data[e]||{},o=[];if(t.typeInfo[e]&&t.typeInfo[e].controls&&t.typeInfo[e].controls.forEach(function(e){"relation"===e.controlType?o.push({ownerField:null,name:e.name,isSingle:e.meta?e.meta.isSingle:!1}):"grid"===e.controlType&&e.controls&&e.controls.forEach(function(t){"relation"===t.controlType&&o.push({ownerField:e.name,name:t.name,isSingle:t.meta?t.meta.isSingle:!1})})}),t.typeInfo[e]&&t.typeInfo[e].oneOff)return n=l(o,n),void(r=n);n=_.omit(n,function(e,t){return 0===t.indexOf("_")});n=_.map(n,function(n,i){var r="";if(n._id=i,n._type=e,n.name&&!n.slug){var r=a(n),s="";t.typeInfo[e]&&t.typeInfo[e].customUrls&&t.typeInfo[e].customUrls.listUrl?"#"!==t.typeInfo[e].customUrls.listUrl&&(s=t.typeInfo[e].customUrls.listUrl+"/"):s=e+"/",t.typeInfo[e]&&t.typeInfo[e].customUrls&&t.typeInfo[e].customUrls.individualUrl&&(s+=utils.parseCustomUrl(t.typeInfo[e].customUrls.individualUrl,n)+"/"),s+=r,n.slug=s}return n=l(o,n)}),n=_.filter(n,function(e){if(!i&&!e.publish_date)return!1;var t=Date.now(),n=Date.parse(e.publish_date);return n>t+6e4?!1:!0}),r=r.concat(n)}),t.cachedData[e.join(",")+","+i]=r,r},f=function(e,n,i){if(1===t.curPage&&t.paginate===!0)throw new Error("Can only paginate one set of data in a template.");var r=utils.slice(e,n,n*(t.curPage-1));return t.paginate=!0,null===t.paginationBaseUrl&&(t.paginationBaseUrl=t.CURRENT_URL),t.pageUrl=i||t.pageUrl,t.maxPage=Math.ceil(_(e).size()/n),r},u=function(){return t.curPage},m=function(){return t.maxPage},d=function(e){return 1==e?t.paginationBaseUrl:t.paginationBaseUrl+t.pageUrl+e+"/"},h=function(){return t.CURRENT_URL},g=function(e){return t.settings.general?t.settings.general[e]:null},p=function(e){if(!e||!_.isArray(e))return null;var t=[Math.floor(Math.random()*e.length)];return e[t]},k=function(e,t,n){if(0===_.size(e))return e;var i=e[0],r="_sort_"+t;return i[r]&&(t=r),n?_.sortBy(e,t).reverse():_.sortBy(e,t)},v=function(e,t,n){var i=e._type,r=c(i);t&&(r=k(r,t,n));var o=null,s=null;return r.some(function(t){return s&&s._id==e._id?(o=t,!0):void(s=t)}),o},b=function(e,t,n){var i=e._type,r=c(i);t&&(r=k(r,t,n));var o=null,s=null;return r.some(function(t){return t._id==e._id?(o=s,!0):void(s=t)}),o},y=function(){var e=[].slice.call(arguments,0),t=[];return e.forEach(function(e){""!==e&&null!==e&&(t=t.concat(e))}),t};return this.shouldPaginate=function(){return t.curPage<=t.maxPage},this.init=function(){t.paginate=!1,t.curPage=1,t.pageUrl="page-",t.maxPage=-1,t.paginationBaseUrl=null},this.increasePage=function(){t.curPage=t.curPage+1},this.setParams=function(e){for(var n in e)t[n]=e[n]},this.getFunctions=function(){var e={get:c,getItem:function(e,t,n){return"string"==typeof e&&t?r(e,t,n):e},_realGetItem:function(e,t,n){return r(e,t,n)},getItems:function(e){return e},getTypes:i,paginate:f,getCurPage:u,getMaxPage:m,getPageUrl:d,url:n,getCurrentUrl:h,getSetting:g,random:p,cmsVersion:"v2",merge:y,nextItem:v,prevItem:b},o=[];for(var s in t.typeInfo)o.push(s);var a={};return o.forEach(function(e){Object.defineProperty(a,e,{get:function(){return c(e)},enumerable:!0,configurable:!0})}),e.cms=a,Object.defineProperty(e,"cms_types",{get:function(){return i()},enumerable:!0,configurable:!0}),e},this};