/**
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2024 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
; (function ($) {

    /** _agile_  TODO rewrite as a widget, removing all the extra plugins _agile_  **/
    $.extend($.fn, {
        swapClass: function (c1, c2) {
            var c1Elements = this.filter('.' + c1);
            this.filter('.' + c2).removeClass(c2).addClass(c1);
            c1Elements.removeClass(c1).addClass(c2);
            return this;
        },
        replaceClass: function (c1, c2) {
            return this.filter('.' + c1).removeClass(c1).addClass(c2).end();
        },
        hoverClass: function (className) {
            className = className || "hover";
            return this.hover(function () {
                $(this).addClass(className);
            }, function () {
                $(this).removeClass(className);
            });
        },
        heightToggle: function (animated, callback) {
            animated ?
				this.animate({ height: "toggle" }, animated, callback) :
				this.each(function () {
				    jQuery(this)[jQuery(this).is(":hidden") ? "show" : "hide"]();
				    if (callback)
				        callback.apply(this, arguments);
				});
        },
        heightHide: function (animated, callback) {
            if (animated) {
                this.animate({ height: "hide" }, animated, callback);
            } else {
                this.hide();
                if (callback)
                    this.each(callback);
            }
        },
        prepareBranches: function (settings) {
            if (!settings.prerendered) {
                /** _agile_  mark last tree items _agile_  **/
                this.filter(":last-child:not(ul)").addClass(CLASSES.last);
                /** _agile_  collapse whole tree, or only those marked as closed, anyway except those marked as open _agile_  **/
                this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
            }
            /** _agile_  return all items with sublists _agile_  **/
            return this.filter(":has(>ul)");
        },
        applyClasses: function (settings, toggler) {
            /** _agile_  TODO use event delegation _agile_  **/
            this.filter(":has(>ul):not(:has(>a))").find(">span").unbind("click.treeview").bind("click.treeview", function (event) {
                /** _agile_  don't handle click events on children, eg. checkboxes _agile_  **/
                if (this == event.target)
                    toggler.apply($(this).next());
            }).add($("a", this)).hoverClass();

            if (!settings.prerendered) {
                /** _agile_  handle closed ones first _agile_  **/
                this.filter(":has(>ul:hidden)")
						.addClass(CLASSES.expandable)
						.replaceClass(CLASSES.last, CLASSES.lastExpandable);

                /** _agile_  handle open ones _agile_  **/
                this.not(":has(>ul:hidden)")
						.addClass(CLASSES.collapsable)
						.replaceClass(CLASSES.last, CLASSES.lastCollapsable);

                /** _agile_  create hitarea if not present _agile_  **/
                var hitarea = this.find("div." + CLASSES.hitarea);
                if (!hitarea.length)
                    hitarea = this.prepend("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea);
                hitarea.removeClass().addClass(CLASSES.hitarea).each(function () {
                    var classes = "";
                    $.each($(this).parent().attr("class").split(" "), function () {
                        classes += this + "-hitarea ";
                    });
                    $(this).addClass(classes);
                })
            }

            /** _agile_  apply event to hitarea _agile_  **/
            this.find("div." + CLASSES.hitarea).click(toggler);
        },
        treeview: function (settings) {

            settings = $.extend({
                cookieId: "treeview"
            }, settings);

            if (settings.toggle) {
                var callback = settings.toggle;
                settings.toggle = function () {
                    return callback.apply($(this).parent()[0], arguments);
                };
            }

            /** _agile_  factory for treecontroller _agile_  **/
            function treeController(tree, control) {
                /** _agile_  factory for click handlers _agile_  **/
                function handler(filter) {
                    return function () {
                        /** _agile_  reuse toggle event handler, applying the elements to toggle _agile_  **/
                        /** _agile_  start searching for all hitareas _agile_  **/
                        toggler.apply($("div." + CLASSES.hitarea, tree).filter(function () {
                            /** _agile_  for plain toggle, no filter is provided, otherwise we need to check the parent element _agile_  **/
                            return filter ? $(this).parent("." + filter).length : true;
                        }));
                        return false;
                    };
                }
                /** _agile_  click on first element to collapse tree _agile_  **/
                $("a:eq(0)", control).click(handler(CLASSES.collapsable));
                /** _agile_  click on second to expand tree _agile_  **/
                $("a:eq(1)", control).click(handler(CLASSES.expandable));
                /** _agile_  click on third to toggle tree _agile_  **/
                $("a:eq(2)", control).click(handler());
            }

            /** _agile_  handle toggle event _agile_  **/
            function toggler() {
                /** _agile_  Added by Prestashop _agile_  **/
                if ($(this).parent().is('.static'))
                    return;
                $(this)
					.parent()
					/** _agile_  swap classes for hitarea _agile_  **/
					.find(">.hitarea")
						.swapClass(CLASSES.collapsableHitarea, CLASSES.expandableHitarea)
						.swapClass(CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea)
					.end()
					/** _agile_  swap classes for parent li _agile_  **/
					.swapClass(CLASSES.collapsable, CLASSES.expandable)
					.swapClass(CLASSES.lastCollapsable, CLASSES.lastExpandable)
					/** _agile_  find child lists _agile_  **/
					.find(">ul")
					/** _agile_  toggle them _agile_  **/
					.heightToggle(settings.animated, settings.toggle);
                if (settings.unique) {
                    $(this).parent()
						.siblings()
						/** _agile_  swap classes for hitarea _agile_  **/
						.find(">.hitarea")
							.replaceClass(CLASSES.collapsableHitarea, CLASSES.expandableHitarea)
							.replaceClass(CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea)
						.end()
						.replaceClass(CLASSES.collapsable, CLASSES.expandable)
						.replaceClass(CLASSES.lastCollapsable, CLASSES.lastExpandable)
						.find(">ul")
						.heightHide(settings.animated, settings.toggle);
                }
            }
            this.data("toggler", toggler);

            function serialize() {
                function binary(arg) {
                    return arg ? 1 : 0;
                }
                var data = [];
                branches.each(function (i, e) {
                    data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
                });
                $.cookie(settings.cookieId, data.join(""), settings.cookieOptions);
            }

            function deserialize() {
                var stored = $.cookie(settings.cookieId);
                if (stored) {
                    var data = stored.split("");
                    branches.each(function (i, e) {
                        $(e).find(">ul")[parseInt(data[i]) ? "show" : "hide"]();
                    });
                }
            }

            /** _agile_  add treeview class to activate styles _agile_  **/
            this.addClass("treeview");

            /** _agile_  prepare branches and find all tree items with child lists _agile_  **/
            var branches = this.find("li").prepareBranches(settings);

            switch (settings.persist) {
                case "cookie":
                    var toggleCallback = settings.toggle;
                    settings.toggle = function () {
                        serialize();
                        if (toggleCallback) {
                            toggleCallback.apply(this, arguments);
                        }
                    };
                    deserialize();
                    break;
                case "location":
                    var current = this.find("a").filter(function () {
                        return this.href.toLowerCase() == location.href.toLowerCase();
                    });
                    if (current.length) {
                        /** _agile_  TODO update the open/closed classes _agile_  **/
                        var items = current.addClass("selected").parents("ul, li").add(current.next()).show();
                        if (settings.prerendered) {
                            /** _agile_  if prerendered is on, replicate the basic class swapping _agile_  **/
                            items.filter("li")
                                .swapClass(CLASSES.collapsable, CLASSES.expandable)
                                .swapClass(CLASSES.lastCollapsable, CLASSES.lastExpandable)
                                .find(">.hitarea")
                                    .swapClass(CLASSES.collapsableHitarea, CLASSES.expandableHitarea)
                                    .swapClass(CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea);
                        }
                    }
                    break;
            }

            branches.applyClasses(settings, toggler);

            /** _agile_  if control option is set, create the treecontroller and show it _agile_  **/
            if (settings.control) {
                treeController(this, settings.control);
                $(settings.control).show();
            }

            return this;
        }
    });

    /** _agile_  classes used by the plugin _agile_  **/
    /** _agile_  need to be styled via external stylesheet, see first example _agile_  **/
    $.treeview = {};
    var CLASSES = ($.treeview.classes = {
        open: "open",
        closed: "closed",
        expandable: "expandable",
        expandableHitarea: "expandable-hitarea",
        lastExpandableHitarea: "lastExpandable-hitarea",
        collapsable: "collapsable",
        collapsableHitarea: "collapsable-hitarea",
        lastCollapsableHitarea: "lastCollapsable-hitarea",
        lastCollapsable: "lastCollapsable",
        lastExpandable: "lastExpandable",
        last: "last",
        hitarea: "hitarea"
    });

})(jQuery);
