import { Component, Inject, ViewEncapsulation } from '@angular/core';
import { MatLegacyDialogRef as MatDialogRef, MAT_LEGACY_DIALOG_DATA } from '@angular/material/legacy-dialog';
import * as i0 from "@angular/core";
import * as i1 from "@angular/material/legacy-dialog";
import * as i2 from "@angular/common";
const _c0 = a0 => ({ cat: a0, level: 1 });
const _c1 = (a0, a1) => ({ cat: a0, level: a1 });
function CaseFileViewFolderSelectorComponent_ng_container_6_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainer(0);
} }
function CaseFileViewFolderSelectorComponent_ng_container_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, CaseFileViewFolderSelectorComponent_ng_container_6_ng_container_1_Template, 1, 0, "ng-container", 8);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const cat_r2 = ctx.$implicit;
    i0.ɵɵnextContext();
    const folder_r3 = i0.ɵɵreference(13);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngTemplateOutlet", folder_r3)("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, cat_r2));
} }
function CaseFileViewFolderSelectorComponent_ng_template_12_ng_container_5_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainer(0);
} }
function CaseFileViewFolderSelectorComponent_ng_template_12_ng_container_5_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, CaseFileViewFolderSelectorComponent_ng_template_12_ng_container_5_ng_container_1_Template, 1, 0, "ng-container", 8);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const subcat_r6 = ctx.$implicit;
    const level_r7 = i0.ɵɵnextContext().level;
    i0.ɵɵnextContext();
    const folder_r3 = i0.ɵɵreference(13);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngTemplateOutlet", folder_r3)("ngTemplateOutletContext", i0.ɵɵpureFunction2(2, _c1, subcat_r6, level_r7 + 1));
} }
function CaseFileViewFolderSelectorComponent_ng_template_12_Template(rf, ctx) { if (rf & 1) {
    const _r4 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "div", 9)(1, "input", 10);
    i0.ɵɵlistener("click", function CaseFileViewFolderSelectorComponent_ng_template_12_Template_input_click_1_listener($event) { i0.ɵɵrestoreView(_r4); const ctx_r4 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r4.handleChange($event)); })("keyup", function CaseFileViewFolderSelectorComponent_ng_template_12_Template_input_keyup_1_listener($event) { i0.ɵɵrestoreView(_r4); const ctx_r4 = i0.ɵɵnextContext(); return i0.ɵɵresetView($event.key === "Enter" ? ctx_r4.handleChange($event) : null); });
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(2, "label", 11);
    i0.ɵɵelement(3, "img", 12);
    i0.ɵɵtext(4);
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(5, CaseFileViewFolderSelectorComponent_ng_template_12_ng_container_5_Template, 2, 5, "ng-container", 4);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const cat_r8 = ctx.cat;
    const level_r7 = ctx.level;
    i0.ɵɵadvance();
    i0.ɵɵpropertyInterpolate1("name", "level-", level_r7, "");
    i0.ɵɵproperty("id", cat_r8.category_id);
    i0.ɵɵadvance();
    i0.ɵɵproperty("for", cat_r8.category_id);
    i0.ɵɵadvance(2);
    i0.ɵɵtextInterpolate1(" ", cat_r8.category_name, " ");
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngForOf", cat_r8.sub_categories);
} }
export class CaseFileViewFolderSelectorComponent {
    dialogRef;
    data;
    currentCategories = [];
    selected = '';
    constructor(dialogRef, data) {
        this.dialogRef = dialogRef;
        this.data = data;
        this.currentCategories = [...this.data.categories];
    }
    ngAfterViewInit() {
        const path = this.findPath();
        path.forEach((p) => document.getElementById(p).checked = true);
    }
    handleChange(evt) {
        if (evt.target.checked) {
            this.select(evt.target.id);
            // get level of this checkbox so we can clear all lower levels
            let level = parseInt(evt.target.name.split('-')[1], 10) + 1;
            let nodes = document.getElementsByName(`level-${level}`);
            while (nodes.length > 0) {
                nodes.forEach((node) => node.checked = false);
                level += 1;
                nodes = document.getElementsByName(`level-${level}`);
            }
        }
    }
    select(categoryId) {
        this.selected = categoryId;
    }
    cancel() {
        this.dialogRef.close();
    }
    save() {
        this.dialogRef.close(this.selected.length > 0 ? this.selected : null);
    }
    findPath() {
        for (const c of this.data.categories) {
            const r = this.containsDocument(c, this.data.document);
            if (r) {
                return r;
            }
        }
        return [];
    }
    containsDocument(cat, document) {
        if (cat.documents.findIndex((doc) => (doc.document_binary_url === document.document_binary_url)
            && (doc.attribute_path === document.attribute_path)) > -1) {
            return [cat.category_id];
        }
        for (const c of cat.sub_categories) {
            const r = this.containsDocument(c, document);
            if (r) {
                return [cat.category_id, ...r];
            }
        }
        return null;
    }
    static ɵfac = function CaseFileViewFolderSelectorComponent_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || CaseFileViewFolderSelectorComponent)(i0.ɵɵdirectiveInject(i1.MatLegacyDialogRef), i0.ɵɵdirectiveInject(MAT_LEGACY_DIALOG_DATA)); };
    static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: CaseFileViewFolderSelectorComponent, selectors: [["xui-case-file-view-folder-selector"]], decls: 14, vars: 2, consts: [["folder", ""], ["tabindex", "0", "aria-label", "close", 1, "close", 3, "click", "keyup"], [1, "govuk-heading-l"], [1, "folders", "govuk-radios", "govuk-radios--conditional"], [4, "ngFor", "ngForOf"], [1, "govuk-button-group"], ["type", "button", 1, "button", "button-primary", 3, "click"], ["href", "#", 1, "cancel", 3, "click"], [4, "ngTemplateOutlet", "ngTemplateOutletContext"], [1, "folder", "govuk-radios__item"], ["type", "radio", 1, "govuk-radios__input", 3, "click", "keyup", "name", "id"], [1, "govuk-label", "govuk-radios__label", 3, "for"], ["src", "/assets/images/folder.png", "alt", "Folder icon", 1, "iconImg"]], template: function CaseFileViewFolderSelectorComponent_Template(rf, ctx) { if (rf & 1) {
            const _r1 = i0.ɵɵgetCurrentView();
            i0.ɵɵelementStart(0, "span", 1);
            i0.ɵɵlistener("click", function CaseFileViewFolderSelectorComponent_Template_span_click_0_listener() { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.cancel()); })("keyup", function CaseFileViewFolderSelectorComponent_Template_span_keyup_0_listener($event) { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView($event.key === "Enter" ? ctx.cancel() : null); });
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(1, "h2", 2);
            i0.ɵɵtext(2, "Move File");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "p");
            i0.ɵɵtext(4);
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(5, "div", 3);
            i0.ɵɵtemplate(6, CaseFileViewFolderSelectorComponent_ng_container_6_Template, 2, 4, "ng-container", 4);
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(7, "div", 5)(8, "button", 6);
            i0.ɵɵlistener("click", function CaseFileViewFolderSelectorComponent_Template_button_click_8_listener() { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.save()); });
            i0.ɵɵtext(9, "Save");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(10, "a", 7);
            i0.ɵɵlistener("click", function CaseFileViewFolderSelectorComponent_Template_a_click_10_listener($event) { i0.ɵɵrestoreView(_r1); ctx.cancel(); return i0.ɵɵresetView($event.preventDefault()); });
            i0.ɵɵtext(11, "Cancel");
            i0.ɵɵelementEnd()();
            i0.ɵɵtemplate(12, CaseFileViewFolderSelectorComponent_ng_template_12_Template, 6, 6, "ng-template", null, 0, i0.ɵɵtemplateRefExtractor);
        } if (rf & 2) {
            i0.ɵɵadvance(4);
            i0.ɵɵtextInterpolate1("Where do you want to move \"", ctx.data.document.document_filename, "\" file?");
            i0.ɵɵadvance(2);
            i0.ɵɵproperty("ngForOf", ctx.currentCategories);
        } }, dependencies: [i2.NgForOf, i2.NgTemplateOutlet], styles: [":host{position:relative;display:block}.folders{margin-bottom:20px}.folder{display:none;border-top:1px solid #CCC;margin:0 0 -1px}.folder:last-child{border-bottom:1px solid #CCC}.folders>.folder{display:block}input:checked~.folder{display:block}.iconImg{position:relative;display:inline-block;height:28px}.folder>.folder{margin-left:20px}.folder label:before{top:3px}.folder label:after{top:13px}p{font-weight:700}button{margin-right:20px}.close{float:right;width:30px;height:30px;background:url(/assets/img/x.PNG) no-repeat center center;cursor:pointer}.cancel{cursor:pointer}.cdk-overlay-pane{overflow:auto;background:#fff}.cdk-overlay-pane::-webkit-scrollbar{width:7px}.cdk-overlay-pane::-webkit-scrollbar-thumb{border:4px solid rgba(0,0,0,0);background-clip:padding-box;border-radius:9999px;background-color:#aaa}.cdk-overlay-pane::-webkit-scrollbar-button{display:none}.cdk-overlay-pane::-webkit-scrollbar-track-piece{background:#eee}.cdk-overlay-pane::-webkit-scrollbar-thumb{background:#ccc}\n"], encapsulation: 2 });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(CaseFileViewFolderSelectorComponent, [{
        type: Component,
        args: [{ selector: 'xui-case-file-view-folder-selector', encapsulation: ViewEncapsulation.None, template: "<span tabindex=\"0\" aria-label=\"close\" class=\"close\" (click)=\"cancel()\" (keyup)=\"$event.key === 'Enter' ? cancel() : null\"></span>\n<h2 class=\"govuk-heading-l\">Move File</h2>\n<p>Where do you want to move \"{{ data.document.document_filename }}\" file?</p>\n<div class=\"folders govuk-radios govuk-radios--conditional\">\n    <ng-container *ngFor=\"let cat of currentCategories\">\n        <ng-container *ngTemplateOutlet=\"folder;context:{cat:cat,level:1}\"></ng-container>\n    </ng-container>\n</div>\n<div class=\"govuk-button-group\">\n    <button class=\"button button-primary\" type=\"button\" (click)=\"save()\">Save</button>\n    <a (click)=\"cancel(); $event.preventDefault()\" class=\"cancel\" href=\"#\">Cancel</a>\n</div>\n\n<ng-template #folder let-cat=\"cat\" let-level=\"level\">\n    <div class=\"folder govuk-radios__item\">\n        <input class=\"govuk-radios__input\" type=\"radio\" name=\"level-{{level}}\" [id]=\"cat.category_id\" (click)=\"handleChange($event)\" (keyup)=\"$event.key === 'Enter' ? handleChange($event) : null\" />\n        <label class=\"govuk-label govuk-radios__label\" [for]=\"cat.category_id\">\n            <img class=\"iconImg\" src=\"/assets/images/folder.png\" alt=\"Folder icon\" />\n            {{ cat.category_name }}\n        </label>\n        <ng-container *ngFor=\"let subcat of cat.sub_categories\">\n            <ng-container *ngTemplateOutlet=\"folder;context:{cat:subcat,level:level+1}\"></ng-container>\n        </ng-container>\n    </div>\n</ng-template>", styles: [":host{position:relative;display:block}.folders{margin-bottom:20px}.folder{display:none;border-top:1px solid #CCC;margin:0 0 -1px}.folder:last-child{border-bottom:1px solid #CCC}.folders>.folder{display:block}input:checked~.folder{display:block}.iconImg{position:relative;display:inline-block;height:28px}.folder>.folder{margin-left:20px}.folder label:before{top:3px}.folder label:after{top:13px}p{font-weight:700}button{margin-right:20px}.close{float:right;width:30px;height:30px;background:url(/assets/img/x.PNG) no-repeat center center;cursor:pointer}.cancel{cursor:pointer}.cdk-overlay-pane{overflow:auto;background:#fff}.cdk-overlay-pane::-webkit-scrollbar{width:7px}.cdk-overlay-pane::-webkit-scrollbar-thumb{border:4px solid rgba(0,0,0,0);background-clip:padding-box;border-radius:9999px;background-color:#aaa}.cdk-overlay-pane::-webkit-scrollbar-button{display:none}.cdk-overlay-pane::-webkit-scrollbar-track-piece{background:#eee}.cdk-overlay-pane::-webkit-scrollbar-thumb{background:#ccc}\n"] }]
    }], () => [{ type: i1.MatLegacyDialogRef }, { type: undefined, decorators: [{
                type: Inject,
                args: [MAT_LEGACY_DIALOG_DATA]
            }] }], null); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(CaseFileViewFolderSelectorComponent, { className: "CaseFileViewFolderSelectorComponent", filePath: "lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.ts", lineNumber: 12 }); })();
//# sourceMappingURL=data:application/json;base64, 