/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};define(["require","exports","jquery","../AbstractInteractableModule","TYPO3/CMS/Backend/Modal","TYPO3/CMS/Backend/Notification","TYPO3/CMS/Core/Ajax/AjaxRequest","../../Renderable/InfoBox","../../Renderable/ProgressBar","../../Renderable/Severity","../../Router"],(function(e,t,s,a,n,o,r,l,i,c,d){"use strict";s=__importDefault(s);class u extends a.AbstractInteractableModule{constructor(){super(...arguments),this.selectorAnalyzeTrigger=".t3js-databaseAnalyzer-analyze",this.selectorExecuteTrigger=".t3js-databaseAnalyzer-execute",this.selectorOutputContainer=".t3js-databaseAnalyzer-output",this.selectorSuggestionBlock=".t3js-databaseAnalyzer-suggestion-block",this.selectorSuggestionList=".t3js-databaseAnalyzer-suggestion-list",this.selectorSuggestionLineTemplate=".t3js-databaseAnalyzer-suggestion-line-template"}initialize(e){this.currentModal=e,this.getData(),e.on("click",".t3js-databaseAnalyzer-suggestion-block-checkbox",e=>{const t=(0,s.default)(e.currentTarget);t.closest("fieldset").find(":checkbox").prop("checked",t.get(0).checked)}),e.on("click",this.selectorAnalyzeTrigger,e=>{e.preventDefault(),this.analyze()}),e.on("click",this.selectorExecuteTrigger,e=>{e.preventDefault(),this.execute()})}getData(){const e=this.getModalBody();new r(d.getUrl("databaseAnalyzer")).get({cache:"no-cache"}).then(async t=>{const s=await t.resolve();!0===s.success?(e.empty().append(s.html),n.setButtons(s.buttons),this.analyze()):o.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{d.handleAjaxError(t,e)})}analyze(){this.setModalButtonsState(!1);const e=this.getModalBody(),t=this.getModalFooter(),s=e.find(this.selectorOutputContainer),a=t.find(this.selectorExecuteTrigger),n=t.find(this.selectorAnalyzeTrigger);s.empty().append(i.render(c.loading,"Analyzing current database schema...","")),s.on("change",'input[type="checkbox"]',()=>{const e=s.find(":checked").length>0;this.setModalButtonState(a,e)}),new r(d.getUrl("databaseAnalyzerAnalyze")).get({cache:"no-cache"}).then(async t=>{const r=await t.resolve();!0===r.success?(Array.isArray(r.status)&&(s.find(".alert-loading").remove(),r.status.forEach(e=>{const t=l.render(e.severity,e.title,e.message);s.append(t)})),Array.isArray(r.suggestions)&&(r.suggestions.forEach(t=>{const a=e.find(this.selectorSuggestionBlock).clone();a.removeClass(this.selectorSuggestionBlock.substr(1));const n=t.key;a.find(".t3js-databaseAnalyzer-suggestion-block-legend").text(t.label),a.find(".t3js-databaseAnalyzer-suggestion-block-checkbox").attr("id","t3-install-"+n+"-checkbox"),t.enabled&&a.find(".t3js-databaseAnalyzer-suggestion-block-checkbox").attr("checked","checked"),a.find(".t3js-databaseAnalyzer-suggestion-block-label").attr("for","t3-install-"+n+"-checkbox"),t.children.forEach(s=>{const n=e.find(this.selectorSuggestionLineTemplate).children().clone(),o=s.hash,r=n.find(".t3js-databaseAnalyzer-suggestion-line-checkbox");r.attr("id","t3-install-db-"+o).attr("data-hash",o),t.enabled&&r.attr("checked","checked"),n.find(".t3js-databaseAnalyzer-suggestion-line-label").attr("for","t3-install-db-"+o),n.find(".t3js-databaseAnalyzer-suggestion-line-statement").text(s.statement),void 0!==s.current&&(n.find(".t3js-databaseAnalyzer-suggestion-line-current-value").text(s.current),n.find(".t3js-databaseAnalyzer-suggestion-line-current").show()),void 0!==s.rowCount&&(n.find(".t3js-databaseAnalyzer-suggestion-line-count-value").text(s.rowCount),n.find(".t3js-databaseAnalyzer-suggestion-line-count").show()),a.find(this.selectorSuggestionList).append(n)}),s.append(a.html())}),this.setModalButtonState(n,!0),this.setModalButtonState(a,s.find(":checked").length>0)),0===r.suggestions.length&&0===r.status.length&&s.append(l.render(c.ok,"Database schema is up to date. Good job!",""))):o.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{d.handleAjaxError(t,e)})}execute(){this.setModalButtonsState(!1);const e=this.getModalBody(),t=this.getModuleContent().data("database-analyzer-execute-token"),a=e.find(this.selectorOutputContainer),n=[];a.find(".t3js-databaseAnalyzer-suggestion-line input:checked").each((e,t)=>{n.push((0,s.default)(t).data("hash"))}),a.empty().append(i.render(c.loading,"Executing database updates...","")),new r(d.getUrl()).post({install:{action:"databaseAnalyzerExecute",token:t,hashes:n}}).then(async e=>{const t=await e.resolve();Array.isArray(t.status)&&t.status.forEach(e=>{o.showMessage(e.title,e.message,e.severity)}),this.analyze()},t=>{d.handleAjaxError(t,e)})}}return new u}));