define(["jquery","core/templates","core/ajax","core/notification","core/str","tool_lp/menubar","tool_lp/dialogue"],function(a,b,c,d,e,f,g){var h=function(a){if(this._type=a,"plan"===a)this._region='[data-region="plan-page"]',this._planNode='[data-region="plan-page"]',this._template="tool_lp/plan_page",this._contextMethod="tool_lp_data_for_plan_page";else{if("plans"!==a)throw new TypeError("Unexpected type.");this._region='[data-region="plans"]',this._planNode='[data-region="plan-node"]',this._template="tool_lp/plans_page",this._contextMethod="tool_lp_data_for_plans_page"}};return h.prototype._contextMethod=null,h.prototype._planNode=null,h.prototype._region=null,h.prototype._template=null,h.prototype._type=null,h.prototype._getContextArgs=function(a){var b=this,c={};return"plan"===b._type?c={planid:a.id}:"plans"===b._type&&(c={userid:a.userid}),c},h.prototype.refresh=function(b){var c=this._findPlanData(a(b));this._callAndRefresh([],c)},h.prototype._renderView=function(c){var d=this;return b.render(d._template,c).then(function(c,e){a(d._region).replaceWith(c),b.runTemplateJS(e)})},h.prototype._callAndRefresh=function(b,e){var f="tool_lp/planactions:_callAndRefresh-"+Math.floor(Math.random()*Math.floor(1e3));M.util.js_pending(f);var g=this;return b.push({methodname:g._contextMethod,args:g._getContextArgs(e)}),a.when.apply(a,c.call(b)).then(function(){return g._renderView(arguments[arguments.length-1])}).fail(d.exception).always(function(){return M.util.js_complete(f)})},h.prototype._doDelete=function(a){var b=this,c=[{methodname:"core_competency_delete_plan",args:{id:a.id}}];b._callAndRefresh(c,a)},h.prototype.deletePlan=function(a){var b,f=this;b=c.call([{methodname:"core_competency_read_plan",args:{id:a.id}}]),b[0].done(function(b){e.get_strings([{key:"confirm",component:"moodle"},{key:"deleteplan",component:"tool_lp",param:b.name},{key:"delete",component:"moodle"},{key:"cancel",component:"moodle"}]).done(function(b){d.confirm(b[0],b[1],b[2],b[3],function(){f._doDelete(a)})}).fail(d.exception)}).fail(d.exception)},h.prototype._doReopenPlan=function(a){var b=this,c=[{methodname:"core_competency_reopen_plan",args:{planid:a.id}}];b._callAndRefresh(c,a)},h.prototype.reopenPlan=function(a){var b=this,f=c.call([{methodname:"core_competency_read_plan",args:{id:a.id}}]);f[0].done(function(c){e.get_strings([{key:"confirm",component:"moodle"},{key:"reopenplanconfirm",component:"tool_lp",param:c.name},{key:"reopenplan",component:"tool_lp"},{key:"cancel",component:"moodle"}]).done(function(c){d.confirm(c[0],c[1],c[2],c[3],function(){b._doReopenPlan(a)})}).fail(d.exception)}).fail(d.exception)},h.prototype._doCompletePlan=function(a){var b=this,c=[{methodname:"core_competency_complete_plan",args:{planid:a.id}}];b._callAndRefresh(c,a)},h.prototype.completePlan=function(a){var b=this,f=c.call([{methodname:"core_competency_read_plan",args:{id:a.id}}]);f[0].done(function(c){e.get_strings([{key:"confirm",component:"moodle"},{key:"completeplanconfirm",component:"tool_lp",param:c.name},{key:"completeplan",component:"tool_lp"},{key:"cancel",component:"moodle"}]).done(function(c){d.confirm(c[0],c[1],c[2],c[3],function(){b._doCompletePlan(a)})}).fail(d.exception)}).fail(d.exception)},h.prototype._doUnlinkPlan=function(a){var b=this,c=[{methodname:"core_competency_unlink_plan_from_template",args:{planid:a.id}}];b._callAndRefresh(c,a)},h.prototype.unlinkPlan=function(a){var b=this,f=c.call([{methodname:"core_competency_read_plan",args:{id:a.id}}]);f[0].done(function(c){e.get_strings([{key:"confirm",component:"moodle"},{key:"unlinkplantemplateconfirm",component:"tool_lp",param:c.name},{key:"unlinkplantemplate",component:"tool_lp"},{key:"cancel",component:"moodle"}]).done(function(c){d.confirm(c[0],c[1],c[2],c[3],function(){b._doUnlinkPlan(a)})}).fail(d.exception)}).fail(d.exception)},h.prototype._doRequestReview=function(a){var b=[{methodname:"core_competency_plan_request_review",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.requestReview=function(a){this._doRequestReview(a)},h.prototype._doCancelReviewRequest=function(a){var b=[{methodname:"core_competency_plan_cancel_review_request",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.cancelReviewRequest=function(a){this._doCancelReviewRequest(a)},h.prototype._doStartReview=function(a){var b=[{methodname:"core_competency_plan_start_review",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.startReview=function(a){this._doStartReview(a)},h.prototype._doStopReview=function(a){var b=[{methodname:"core_competency_plan_stop_review",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.stopReview=function(a){this._doStopReview(a)},h.prototype._doApprove=function(a){var b=[{methodname:"core_competency_approve_plan",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.approve=function(a){this._doApprove(a)},h.prototype._doUnapprove=function(a){var b=[{methodname:"core_competency_unapprove_plan",args:{id:a.id}}];this._callAndRefresh(b,a)},h.prototype.unapprove=function(a){this._doUnapprove(a)},h.prototype._showLinkedCoursesHandler=function(f){f.preventDefault();var h=a(f.target).data("id"),i=c.call([{methodname:"tool_lp_list_courses_using_competency",args:{id:h}}]);i[0].done(function(a){var c={courses:a};b.render("tool_lp/linked_courses_summary",c).done(function(a){e.get_string("linkedcourses","tool_lp").done(function(b){new g(b,a)}).fail(d.exception)}).fail(d.exception)}).fail(d.exception)},h.prototype._eventHandler=function(b,c){c.preventDefault();var d=this._findPlanData(a(c.target));this[b](d)},h.prototype._findPlanData=function(b){var c,d=b.parentsUntil(a(this._region).parent(),this._planNode);if(1!=d.length)throw new Error("The plan node was not located.");if(c=d.data(),"undefined"==typeof c||"undefined"==typeof c.id)throw new Error("Plan data could not be found.");return c},h.prototype.enhanceMenubar=function(a){f.enhance(a,{'[data-action="plan-delete"]':this._eventHandler.bind(this,"deletePlan"),'[data-action="plan-complete"]':this._eventHandler.bind(this,"completePlan"),'[data-action="plan-reopen"]':this._eventHandler.bind(this,"reopenPlan"),'[data-action="plan-unlink"]':this._eventHandler.bind(this,"unlinkPlan"),'[data-action="plan-request-review"]':this._eventHandler.bind(this,"requestReview"),'[data-action="plan-cancel-review-request"]':this._eventHandler.bind(this,"cancelReviewRequest"),'[data-action="plan-start-review"]':this._eventHandler.bind(this,"startReview"),'[data-action="plan-stop-review"]':this._eventHandler.bind(this,"stopReview"),'[data-action="plan-approve"]':this._eventHandler.bind(this,"approve"),'[data-action="plan-unapprove"]':this._eventHandler.bind(this,"unapprove")})},h.prototype.registerEvents=function(){var b=a(this._region);b.find('[data-action="plan-delete"]').click(this._eventHandler.bind(this,"deletePlan")),b.find('[data-action="plan-complete"]').click(this._eventHandler.bind(this,"completePlan")),b.find('[data-action="plan-reopen"]').click(this._eventHandler.bind(this,"reopenPlan")),b.find('[data-action="plan-unlink"]').click(this._eventHandler.bind(this,"unlinkPlan")),b.find('[data-action="plan-request-review"]').click(this._eventHandler.bind(this,"requestReview")),b.find('[data-action="plan-cancel-review-request"]').click(this._eventHandler.bind(this,"cancelReviewRequest")),b.find('[data-action="plan-start-review"]').click(this._eventHandler.bind(this,"startReview")),b.find('[data-action="plan-stop-review"]').click(this._eventHandler.bind(this,"stopReview")),b.find('[data-action="plan-approve"]').click(this._eventHandler.bind(this,"approve")),b.find('[data-action="plan-unapprove"]').click(this._eventHandler.bind(this,"unapprove")),b.find('[data-action="find-courses-link"]').click(this._showLinkedCoursesHandler.bind(this))},h});