document.addEventListener("DOMContentLoaded", function () {

    const widget = document.querySelector(".home-used-machines");

    if (!widget) {
        return;
    }


    const listings = widget.querySelector(".hp-listings");
    const row = widget.querySelector(".hp-row");


    if (!listings || !row) {
        return;
    }


    /*
    Prevent duplicate initialization
    */

    if (widget.querySelector(".pk-slider")) {
        return;
    }


    /* =====================================================
       1. CREATE SLIDER STRUCTURE
    ===================================================== */


    const slider = document.createElement("div");

    slider.className = "pk-slider swiper";


    const wrapper = document.createElement("div");

    wrapper.className = "swiper-wrapper";


    slider.appendChild(wrapper);



    /* =====================================================
       2. COLLECT EXISTING HIVEPRESS CARDS
    ===================================================== */


    const cards = Array.from(row.children);


    if (!cards.length) {
        return;
    }



    /* =====================================================
       3. HELPER — EXTRACT ATTRIBUTE VALUE
    ===================================================== */


    function getAttributeValue(attribute) {

        if (!attribute) {
            return "";
        }


        /*
        First try common HivePress value containers
        */

        const valueElement = attribute.querySelector(
            ".hp-listing__attribute-value, .hp-field__value"
        );


        if (valueElement) {

            return valueElement.textContent
                .replace(/\s+/g, " ")
                .trim();

        }


        /*
        Fallback:
        Clone the attribute and remove labels
        */

        const clone = attribute.cloneNode(true);


        clone.querySelectorAll(
            ".hp-listing__attribute-label, .hp-field__label, strong"
        ).forEach(function (label) {

            label.remove();

        });


        return clone.textContent
            .replace(/\s+/g, " ")
            .trim();

    }



    /* =====================================================
       4. BUILD MOBILE SPECS ROW
    ===================================================== */


    function buildMobileSpecsRow(card) {


        const content = card.querySelector(
            ".hp-listing__content"
        );


        if (!content) {
            return;
        }


        /*
        Prevent duplicate mobile row
        */

        if (content.querySelector(".pk-mobile-specs")) {
            return;
        }



        /*
        Find original HivePress attributes
        */

        const typeAttribute = card.querySelector(
            ".hp-listing__attribute--type"
        );


        const modelAttribute = card.querySelector(
            ".hp-listing__attribute--model"
        );


        const makeAttribute = card.querySelector(
            ".hp-listing__attribute--make"
        );



        /*
        Read values
        */

        const typeValue =
            getAttributeValue(typeAttribute);


        const modelValue =
            getAttributeValue(modelAttribute);


        const makeValue =
            getAttributeValue(makeAttribute);



        /*
        Create mobile specs container
        */

        const specsRow =
            document.createElement("div");


        specsRow.className =
            "pk-mobile-specs";



        /*
        Required sequence:

        TYPE | MODEL | MAKE
        */

        const specs = [

            {
                className:
                    "pk-mobile-spec pk-mobile-spec--type",

                value:
                    typeValue
            },


            {
                className:
                    "pk-mobile-spec pk-mobile-spec--model",

                value:
                    modelValue
            },


            {
                className:
                    "pk-mobile-spec pk-mobile-spec--make",

                value:
                    makeValue
            }

        ];



        specs.forEach(function (spec) {


            if (!spec.value) {
                return;
            }


            const item =
                document.createElement("span");


            item.className =
                spec.className;


            item.textContent =
                spec.value;


            specsRow.appendChild(item);

        });



        /*
        Insert only if at least one value exists
        */

        if (specsRow.children.length) {

            content.appendChild(specsRow);

        }

    }



    /* =====================================================
       5. PREPARE AND MOVE CARDS
    ===================================================== */


    cards.forEach(function (card) {


        /*
        Remove HivePress grid column classes
        */

        Array.from(card.classList).forEach(
            function (className) {


                if (className.startsWith("hp-col-")) {

                    card.classList.remove(className);

                }

            }
        );


        /*
        Convert grid item to Swiper slide
        */

        card.classList.add("swiper-slide");



        /*
        Create mobile specs structure
        before moving card
        */

        buildMobileSpecsRow(card);



        /*
        Move existing card into slider
        */

        wrapper.appendChild(card);

    });



    /* =====================================================
       6. CREATE REAL NAVIGATION ELEMENTS
    ===================================================== */


    const previousButton =
        document.createElement("div");


    previousButton.className =
        "swiper-button-prev";



    const nextButton =
        document.createElement("div");


    nextButton.className =
        "swiper-button-next";



    slider.appendChild(previousButton);

    slider.appendChild(nextButton);



    /* =====================================================
       7. HIDE EMPTY ORIGINAL ROW
    ===================================================== */


    row.style.display = "none";



    /* =====================================================
       8. ADD SLIDER TO PAGE
    ===================================================== */


    listings.appendChild(slider);



    /* =====================================================
       9. INITIALIZE SWIPER
    ===================================================== */


    new Swiper(slider, {

        loop: false,

        speed: 500,

        slidesPerView: 4,

        spaceBetween: 20,

        grabCursor: true,


        navigation: {

            nextEl: nextButton,

            prevEl: previousButton

        },


        breakpoints: {


            /*
            MOBILE
            2 cards
            */

            0: {

                slidesPerView: 1.2,

                spaceBetween: 12

            },


            /*
            TABLET
            3 cards
            */

            768: {

                slidesPerView: 3,

                spaceBetween: 18

            },


            /*
            DESKTOP
            4 cards
            */

            1200: {

                slidesPerView: 4,

                spaceBetween: 20

            }

        }

    });


    console.log(

        "Home Used Machines Slider Initialized"
    );

/* Mark original attribute wrappers for mobile hiding */

[
    typeAttribute,
    modelAttribute,
    makeAttribute

].forEach(function(attribute) {

    if (!attribute) {
        return;
    }

    const wrapper = attribute.closest(
        ".hp-col-lg-6, .hp-col-sm-6, .hp-col-xs-12"
    );

    if (wrapper) {
        wrapper.classList.add("pk-mobile-hidden-attribute");
    }

});


});


