class o{constructor(e,t){this.selector=t,$.jstree.defaults.dnd.touch=!0,$.jstree.defaults.dnd.copy=!1,this.fetchCategoryTree(),t.on("select_node.jstree",(a,r)=>e.fetchCategory(r.selected[0])),t.on("loaded.jstree",()=>t.jstree("open_all")),this.selector.on("move_node.jstree",(a,r)=>{this.updateCategoryTree(r)})}fetchCategoryTree(){this.selector.jstree({core:{data:{url:"/admin/categories/tree"},check_callback:!0},plugins:["dnd"]})}updateCategoryTree(e){this.loading(e.node,!0),axios.put("/categories/tree",{category_tree:this.getCategoryTree()}).then(t=>{success(t.data),this.loading(e.node,!1)}).catch(t=>{t(t.response.data.message),this.loading(e.node,!1)})}getCategoryTree(){return this.selector.jstree(!0).get_json("#",{flat:!0}).reduce((t,a)=>t.concat({id:a.id,parent_id:a.parent==="#"?null:a.parent,position:a.data.position}),[])}loading(e,t){let a=this.selector.jstree().get_node(e,!0);t?$(a).addClass("jstree-loading"):$(a).removeClass("jstree-loading")}}class l{constructor(){let e=$(".category-tree");new o(this,e),this.collapseAll(e),this.expandAll(e),this.addRootCategory(),this.addSubCategory(),this.removeSubmitButtonOffsetOn("#image",".category-details-tab li > a"),$("#category-form").on("submit",this.submit)}collapseAll(e){$(".collapse-all").on("click",t=>{t.preventDefault(),e.jstree("close_all")})}expandAll(e){$(".expand-all").on("click",t=>{t.preventDefault(),e.jstree("open_all")})}addRootCategory(){$(".add-root-category").on("click",()=>{this.loading(!0),$(".add-sub-category").addClass("disabled"),$(".category-tree").jstree("deselect_all"),this.clear(),setTimeout(this.loading,150,!1)})}addSubCategory(){$(".add-sub-category").on("click",()=>{let e=$(".category-tree").jstree("get_selected")[0];e!==void 0&&(this.clear(),this.loading(!0),window.form.appendHiddenInput("#category-form","parent_id",e),setTimeout(this.loading,150,!1))})}fetchCategory(e){this.loading(!0),$(".add-sub-category").removeClass("disabled"),axios.get(`/categories/${e}`).then(t=>{this.update(t.data),this.loading(!1)}).catch(t=>{t(t.response.data.message),this.loading(!1)})}update(e){window.form.removeErrors(),$(".btn-delete").removeClass("hide"),$(".form-group .help-block").remove(),$("#confirmation-form").attr("action",`${window.FleetCart.baseUrl}/admin/categories/${e.id}`),$("#id-field").removeClass("hide"),$("#id").val(e.id),$("#name").val(e.name),$("#slug").val(e.slug),$("#slug-field").removeClass("hide"),$(".category-details-tab .seo-tab").removeClass("hide"),$("#is_searchable").prop("checked",e.is_searchable),$("#is_active").prop("checked",e.is_active),$(".logo .image-holder-wrapper").html(this.categoryImage("logo",e.logo)),$(".banner .image-holder-wrapper").html(this.categoryImage("banner",e.banner)),$('#category-form input[name="parent_id"]').remove()}categoryImage(e,t){return t.exists?`
            <div class="image-holder">
                <img src="${t.path}">
                <button type="button" class="btn remove-image" data-input-name="files[${e}]">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
                        <path d="M6.00098 17.9995L17.9999 6.00053" stroke="#292D32" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                        <path d="M17.9999 17.9995L6.00098 6.00055" stroke="#292D32" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </button>
                <input type="hidden" name="files[${e}]" value="${t.id}">
            </div>
        `:this.imagePlaceholder()}clear(){$("#id-field").addClass("hide"),$("#id").val(""),$("#name").val(""),$("#slug").val(""),$("#slug-field").addClass("hide"),$(".category-details-tab .seo-tab").addClass("hide"),$("#is_searchable").prop("checked",!1),$("#is_active").prop("checked",!1),$(".logo .image-holder-wrapper").html(this.imagePlaceholder()),$(".banner .image-holder-wrapper").html(this.imagePlaceholder()),$(".btn-delete").addClass("hide"),$(".form-group .help-block").remove(),$('#category-form input[name="parent_id"]').remove(),$(".general-information-tab a").click()}imagePlaceholder(){return`
            <div class="image-holder placeholder">
                <i class="fa fa-picture-o"></i>
            </div>
        `}loading(e){e===!0?$(".overlay.loader").removeClass("hide"):$(".overlay.loader").addClass("hide")}submit(e){let t=$(".category-tree").jstree("get_selected")[0];$("#slug-field").hasClass("hide")||(window.form.appendHiddenInput("#category-form","_method","put"),$("#category-form").attr("action",`${window.FleetCart.baseUrl}/admin/categories/${t}`)),e.currentTarget.submit()}removeSubmitButtonOffsetOn(e,t=null){e=Array.isArray(e)?e:[e],$(t).on("click",a=>{e.includes(a.currentTarget.getAttribute("href"))?setTimeout(()=>{$("button[type=submit]").parent().removeClass("col-md-offset-3")},150):setTimeout(()=>{$("button[type=submit]").parent().addClass("col-md-offset-3")},150)})}}new l;
