define(["lodash","angular","jquery","cjt/util/locale","cjt/util/query","app/views/Certificate","cjt/modules","app/services/CertificatesService","app/services/LocationService","cjt/directives/spinnerDirective","uiBootstrap"],(function(e,t,r,a,i){"use strict";t.module("App").controller("CheckoutController",["$scope","$controller","$location","$filter","$routeParams","$window","$timeout","CertificatesService","spinnerAPI","$q","$uibModal","$log","Certificate","LocationService","alertService",function(s,n,o,c,d,_,u,l,p,f,g,h,m,v,y){var k={cPStore:["login","send_cart_items","checkout","payment_callback","checkout_complete"],default:["login","send_cart_items","checkout","payment_callback","checkout_complete"]};function w(e){return e.replace(/^\s*-\S+/,"").replace(/-\S+\s*$/,"").replace(/\s+/g,"")}function b(){r(".checkout-step-inner").each((function(e,t){var a=(t=r(t)).find(".content-wrapper"),i=(t.height()-a.height())/2;a.css("padding-top",i)}))}s.pending_certificates=l.get_pending_certificates(),s.LOCALE=a,s.purchase_steps=[],s.current_step=-1,s.start_step=null,s.providers=[],s.certificates_count=0,s.steps=[],s.html_escape=e.escape.bind(e),s.get_step_classes=function(e,t){var r=s.get_steps(e.name).length,a=s.get_step_index(e.name,t),i=Math.floor(12/r),n=["col-xs-12","col-sm-12","col-md-"+i,"col-lg-"+i,"checkout-step"];return s.current_step_index===a?(n.push("checkout-step-current"),"checkout_complete"===t&&n.push("checkout-step-completed")):s.current_step_index>a&&n.push("checkout-step-completed"),n},s.cert_count_title=function(){return a.maketext("Purchasing [quant,_1,certificate,certificates] …",s.certificates_count)},s.get_purchases_title=function(e){return a.maketext("Completing [numerate,_2,purchase,purchases] for the “[_1]” provider …",s.html_escape(e.display_name),e.certificates.length)},s.sending_items_msg=function(){return a.maketext("Sending your [numerate,_1,item,items] to the store cart …",s.certificates_count)},s.starting_polling_msg=function(){return a.maketext("Starting background polling for the [numerate,_1,certificate,certificates]. The system will download and install the [numerate,_1,certificate,certificates] when available.",s.certificates_count)},s.get_provider_by_name=function(e){for(var t=0;t<s.providers.length;t++)if(s.providers[t].name===e)return s.providers[t]},s.get_steps=function(e){return k[e]?k[e]:k.default},s.get_current_step=function(){return s.steps[s.current_step_index]},s.get_step_index=function(e,t){for(var r=0;r<s.steps.length;r++)if(s.steps[r].provider===e&&s.steps[r].step===t)return r;return 0},s.get_step_url=function(e){return"/"+encodeURIComponent(e.provider)+"/"+encodeURIComponent(e.step)},s.get_next_step=function(){if(s.current_step_index+1<s.steps.length)return s.steps[s.current_step_index+1]},s.get_param=function(e){return i.parse_query_string(location.search.replace(/^\?/,""))[e]||d[e]},s.require_params=function(e){var r=[],i=[];return t.forEach(e,(function(e){var t=s.get_param(e);t?t instanceof Array&&i.push(e):r.push(e)})),r.length&&y.add({type:"danger",message:a.maketext("The following [numerate,_1,parameter is,parameters are] required but [numerate,_1,does,do] not appear in the [asis,URL]: [list_and_quoted,_2]",r.length,r),group:"tlsWizard"}),i.length&&y.add({type:"danger",message:a.maketext("The following [numerate,_1,parameter appears,parameters appear] more than once in the [asis,URL]: [list_and_quoted,_2]",i.length,i),group:"tlsWizard"}),!r.length&&!i.length},s.in_debug_mode=!1,s.get_route_url=function(){var e="";return e+=o.absUrl().replace(/tls_wizard\/.+/,"tls_wizard/#/purchase")},s.dismiss_modal=function(){this.modal.dismiss()},s.go_to_purchase_page=v.go_to_last_create_route,s.go_to_login=function(){this.go_step(this.get_current_step().provider,"login")},s.do_current_step=function(){var r=s.get_current_step();if(r){var i,n=s.get_next_step(),c=s.get_param("order_id"),d=s.get_param("code"),p=l.get_order_by_id(c),h=s.get_param("order_status"),m=s.get_provider_by_name(r.provider),k=s.get_param("access_token");if("login"===r.step)i=s.get_route_url()+s.get_step_url(r),p&&(i+="?order_id="+p.order_id),d?l.verify_login_token(r.provider,d,i).then((function(e){p?s.go_step(r.provider,"checkout",{order_id:p.order_id,access_token:e.data.access_token}):s.go_step(r.provider,"send_cart_items",{access_token:e.data.access_token})}),(function(e){s.return_to_wizard(),y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to verify the login token: [_1]",e)+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"})})):l.get_store_login_url(r.provider,i).then((function(e){_.location.href=e.data}),(function(e){s.return_to_wizard(),y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to get the store login [output,abbr,URL,Uniform Resource Location]: [_1]",e)+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"})}));else{if("send_cart_items"===r.step){if(!s.require_params(["access_token"]))return;return i=s.get_route_url()+s.get_step_url(n),l.request_certificates(r.provider,k,m.certificates).then((function(e){var t=e.data;t.order_id=t.order_id.toString(),l.add_order(t),l.save(),s.go_step(r.provider,"checkout",{order_id:t.order_id,access_token:k})}),(function(e){s.return_to_wizard(),y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to request the [asis,SSL] [numerate,_2,certificate,certificates]: [_1]",e,s.get_provider_by_name(r.provider).certificates.length)+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"})}))}if("checkout"===r.step){if(!s.require_params(["order_id"]))return;if(i=s.get_route_url()+s.get_step_url(r),h)s.go_step(r.provider,"payment_callback",{order_id:p.order_id,order_status:h});else{if(!s.require_params(["access_token"]))return;l.set_url_after_checkout(r.provider,k,p.order_id,i).then((function(){_.location.href=p.checkout_url}),(function(t){t.data&&"OrderNotFound"===t.data.error_type?(s.order_id=p.order_id,s.provider=s.get_provider_by_name(r.provider),s.modal=g.open({template:document.getElementById("user-mismatch-modal").text,scope:s,backdrop:"static",animation:!1,size:"sm"})):(v.go_to_last_create_route(),y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to set the [asis,URL] after checkout: [_1]",e.escape(t.error))+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"}))}))}}else if("payment_callback"===r.step){if(CPANEL.PAGE.pending_certificates=null,CPANEL.PAGE.installed_hosts=null,"success"!==h){if("error"===h)l.reset(),l.save(),s.return_to_wizard(),y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to complete your transaction.")+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"});else if(/^cancel?led$/.test(h)){var b=[];t.forEach(p.certificates,(function(e){b.push(e.order_item_id)})),y.add({type:"warn",message:a.maketext("You seem to have canceled your transaction.")+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"}),o.url(o.path()),l.cancel_pending_ssl_certificates(r.provider,b).then((function(){l.reset(),l.save(),s.return_to_wizard()}),(function(e){y.add({type:"danger",message:a.maketext("The system encountered an error as it attempted to cancel your transaction: [_1]",e)+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"})}))}return!1}y.add({type:"success",message:a.maketext("You have successfully completed your certificate order (order ID “[_1]”). If you need help with this order, use the support [numerate,_2,link,links] below.",e.escape(c),p.certificates.length),closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"}),l.set_confirmed_status_for_ssl_certificates(r.provider,p).then((function(){s.go_step(r.provider,n.step)}),(function(t){if(t.data&&"EntryDoesNotExist"===t.data.error_type){var i=t.data.order_item_ids,n=a.maketext("There are no pending certificates from “[_1]” with the following order item [numerate,_2,ID,IDs]: [join,~, ,_3]. The system will now verify that the [numerate,_2,certificate has,certificates have] been issued and installed.",e.escape(r.provider),i.length,i.map(e.escape.bind(e)));y.add({type:"info",message:n,closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"});var o=m.certificates;i.forEach((function(t){var i=l.get_ssl_certificate_if_available(r.provider,t);o.forEach((function(s){s.get_virtual_hosts().forEach((function(n){var o=s.get_domains().filter((function(e){return e.virtual_host===n})).pop().domain;(function(e){if(!(e instanceof Array))throw"Only arrays here!";return f.all(e.map((function(e,t){return f((function(r,a){e.then(r,(function(e){a([t,e])}))}))})))})([l.get_installed_ssl_for_domain(),i]).then((function(i){var s,d=i[0].data.certificate.text;d&&(s=w(d));var _,u=i[1].data.certificate_pem;if(!u){var p,f=i[1].data,g=f.status_code,h=f.status_message;if(/OrderCancell?ed/.test(g))p=a.maketext("“[_1]” indicated that the order with [asis,ID] “[_2]” has been canceled.",e.escape(r.provider),e.escape(c));else if(/OrderItemCancell?ed/.test(g))p=a.maketext("“[_1]” indicated that the certificate with order item [asis,ID] “[_2]” has been canceled.",e.escape(r.provider),e.escape(t));else{p=a.maketext("“[_1]” has not issued a certificate for order item [asis,ID] “[_2]”. Contact them for further assistance.",e.escape(r.provider),e.escape(t));var m=[g,h].filter((function(e){return!!e})).join(": ");m&&(p+=" ("+m+")")}return y.add({type:"danger",message:p,closeable:!0,replace:!1,group:"tlsWizard"}),void v.go_to_last_create_route()}w(u)===s&&(y.add({type:"success",message:a.maketext("The system confirmed that the certificate for the website “[_1]” is installed.",e.escape(n)),closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"}),s?y.add({type:"info",message:a.maketext("“[_1]” has an [asis,SSL] certificate installed, but it is not the certificate that you just ordered (order item [asis,ID] “[_2]”). The system will now install this certificate.",e.escape(n),e.escape(t)),closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"}):(_=a.maketext("You do not have an [asis,SSL] certificate installed for the website “[_1]”.",e.escape(n)),_+=a.maketext("The system will now install the new certificate."),y.add({type:"info",message:_,closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"}),l.install_certificate(u,[o]).then((function(){y.add({type:"success",message:a.maketext("The system installed the certificate onto the website “[_1]”.",e.escape(n)),closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"})}),(function(t){y.add({type:"danger",message:a.maketext("The system failed to install the certificate onto the website “[_1]” because of the following error: [_2]",e.escape(n),t),closeable:!0,replace:!1,group:"tlsWizard"})})).then(v.go_to_last_create_route)))}),(function(i){var s=i[0],o=i[1];0===s?y.add({type:"danger",message:a.maketext("The system failed to locate the installed [asis,SSL] certificate for the website “[_1]” because of the following error: [_2]",e.escape(n),o),closeable:!0,replace:!1,group:"tlsWizard"}):1===s?y.add({type:"danger",message:a.maketext("The system failed to query “[_1]” for order item [asis,ID] “[_2]” ([_3]) because of the following error: [_4]",e.escape(r.provider),e.escape(t),e.escape(n),o),closeable:!0,replace:!1,group:"tlsWizard"}):y.add({type:"danger",message:"Unknown index: "+s,closeable:!0,replace:!1,group:"tlsWizard"}),v.go_to_last_create_route()}))}))}))}))}else{var d=t.error;y.add({type:"danger",message:a.maketext("The system failed to begin polling for [quant,_2,new certificate,new certificates] because of an error: [_1]",d,s.certificates_count)+" "+a.maketext("You will now return to the beginning of the wizard."),closeable:!0,replace:!1,group:"tlsWizard"})}}))}else"checkout_complete"===r.step&&(n||(l.reset(),l.save(),y.add({type:"success",message:a.maketext("The system has completed the [numerate,_1,purchase,purchases] and will begin to poll for your [numerate,_2,certificate,certificates].",s.providers.length,s.certificates_count),closeable:!0,replace:!1,autoClose:1e4,group:"tlsWizard"}),u(s.go_to_pending,1e3)))}}else v.go_to_last_create_route()},s.return_to_wizard=function(){var e=o.absUrl();if(s.get_param("code")){var t=e.replace(/([^#?]+\/).*/,"$1#"+v.last_create_route());_.location.href=t}else v.go_to_last_create_route()},s.check_step_success=function(e){if(e<s.current_step_index)return!0},s.go_step=function(e,t,r){o.path("/purchase/"+e+"/"+t+"/"),r&&o.search(r)},s.get_providers=function(){var e;return s.providers=[],s.purchasing_certs.forEach((function(r){var a=r.get_product(),i=s.get_provider_by_name(a.provider);i||(i={name:a.provider,display_name:a.provider_display_name||a.provider,certificates:[]},s.providers.push(i),e=s.get_steps(i.name),t.forEach(e,(function(e){s.steps.push({provider:i.name,step:e})}))),i.certificates.push(r),s.certificates_count++})),s.providers},s.go_to_pending=function(e){e?o.path("/pending-certificates/").search("orderItemID",e):o.path("/pending-certificates")},s.pending_certificate=function(e){var r=!1;return t.forEach(s.pending_certificates,(function(a){t.forEach(a.vhost_names,(function(t){t===e.display_name&&(r=a.order_item_id)}))})),r},s.view_pending_certificate=function(e){var t=s.pending_certificate(e);s.go_to_pending(t)},s.begin=function(){s.purchasing_certs=l.get_purchasing_certs(),0===s.purchasing_certs.length&&(l.get_virtual_hosts().filter((function(e){if(!e.has_selected_domains())return!1;var t=e.get_product();return t?!!l.get_product_by_id(t.provider,t.id)||(h.warn("Unknown product!",t),!1):(h.warn("has selected, but no product?"),!1)})).forEach((function(e){var t=e.get_product(),r=new m;if(r.set_product(t),r.set_price(e.get_price()),r.set_domains(e.get_selected_domains()),r.set_virtual_hosts([e.display_name]),t.x_identity_verification){var a=e.get_identity_verification();a&&r.set_identity_verification(a)}l.add_new_certificate(r)})),s.purchasing_certs=l.get_purchasing_certs()),s.get_providers(),s.current_provider_name=d.provider,s.current_step_id=d.step,s.current_step_index=s.get_step_index(s.current_provider_name,s.current_step_id),s.do_current_step(),u((function(){b()}),1)},s.init=function(){l.restore(),s.begin()},t.element(_).bind("resize",b),s.init()}])}));