import {createRef, useEffect, useState} from "react";
import {__, CouponsPlus} from "./globals";

const urlsManager = {
    current: null
}
document.addEventListener('CouponURLsInit', ({detail: {manager}}) => {
    urlsManager.current = manager
})

const CouponURLsPanel = () => {
    const ref = createRef()
    const [urlsDashboardManager, setUrlsDashboardManager] = useState(urlsManager.current);

    useEffect(() => {
        /*
            In case of race conditions
         */
        document.addEventListener('CouponURLsInit', ({detail: {manager}}) => {
            setUrlsDashboardManager(manager)
        })
    }, [])

    useEffect(() => {
        if (!CouponsPlus?.compatibility?.couponURLs?.isAvailable) {
            return
        }

        if (!urlsDashboardManager) {
            console.error('urlsDashboardManager is not available')
            return
        }

        const dashboard = urlsDashboardManager.getMainElement()
        ref.current.appendChild(dashboard)
        urlsDashboardManager.open()
        //will run on unmount
        return () => {
            urlsDashboardManager.close()
            document.body.appendChild(dashboard)
        }
    }, [ref, urlsDashboardManager])

    if (!CouponsPlus?.compatibility?.couponURLs?.isAvailable) {
        return <div className="w-full h-full flex flex-col items-center justify-center py-25 px-10">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
                 stroke="currentColor" class="w-12 h-12 text-gray-500">
                <path stroke-linecap="round" stroke-linejoin="round"
                      d="M14.25 6.087c0-.355.186-.676.401-.959.221-.29.349-.634.349-1.003 0-1.036-1.007-1.875-2.25-1.875s-2.25.84-2.25 1.875c0 .369.128.713.349 1.003.215.283.401.604.401.959v0a.64.64 0 01-.657.643 48.39 48.39 0 01-4.163-.3c.186 1.613.293 3.25.315 4.907a.656.656 0 01-.658.663v0c-.355 0-.676-.186-.959-.401a1.647 1.647 0 00-1.003-.349c-1.036 0-1.875 1.007-1.875 2.25s.84 2.25 1.875 2.25c.369 0 .713-.128 1.003-.349.283-.215.604-.401.959-.401v0c.31 0 .555.26.532.57a48.039 48.039 0 01-.642 5.056c1.518.19 3.058.309 4.616.354a.64.64 0 00.657-.643v0c0-.355-.186-.676-.401-.959a1.647 1.647 0 01-.349-1.003c0-1.035 1.008-1.875 2.25-1.875 1.243 0 2.25.84 2.25 1.875 0 .369-.128.713-.349 1.003-.215.283-.4.604-.4.959v0c0 .333.277.599.61.58a48.1 48.1 0 005.427-.63 48.05 48.05 0 00.582-4.717.532.532 0 00-.533-.57v0c-.355 0-.676.186-.959.401-.29.221-.634.349-1.003.349-1.035 0-1.875-1.007-1.875-2.25s.84-2.25 1.875-2.25c.37 0 .713.128 1.003.349.283.215.604.401.96.401v0a.656.656 0 00.658-.663 48.422 48.422 0 00-.37-5.36c-1.886.342-3.81.574-5.766.689a.578.578 0 01-.61-.58v0z"/>
            </svg>
            <h1 className="text-3x text-gray-600 mt-4">{__('Coupon URLs extension needs to be installed!', window.CouponsPlus.textDomain)}</h1>
            <p className="text-base text-gray-450 max-w-135 text-center">{__('Coupons+ works great  with the free Coupon URLs extension. Click on the button bellow to install it.', window.CouponsPlus.textDomain)}</p>
            <a href={CouponsPlus.compatibility.couponURLs.installationURL}
               className={"flex flex-row space-x-1 items-center justify-center bg-blue-normal text-gray-100 px-12 h-8 rounded-1 mt-6"}
               onClick={() => {
               }}>
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
                     stroke="currentColor" class="w-5 h-5">
                    <path stroke-linecap="round" stroke-linejoin="round"
                          d="M9 8.25H7.5a2.25 2.25 0 00-2.25 2.25v9a2.25 2.25 0 002.25 2.25h9a2.25 2.25 0 002.25-2.25v-9a2.25 2.25 0 00-2.25-2.25H15M9 12l3 3m0 0l3-3m-3 3V2.25"/>
                </svg>

                <span className="flex h-4">{__('Install extension', window.CouponsPlus.textDomain)}</span>
            </a>
        </div>
    }
    return <div ref={ref}></div>;
}

CouponURLsPanel.label = __('URL', window.CouponsPlus.textDomain)
CouponURLsPanel.getIcon = () => <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                                     strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
    <path strokeLinecap="round" strokeLinejoin="round"
          d="M12.75 3.03v.568c0 .334.148.65.405.864l1.068.89c.442.369.535 1.01.216 1.49l-.51.766a2.25 2.25 0 01-1.161.886l-.143.048a1.107 1.107 0 00-.57 1.664c.369.555.169 1.307-.427 1.605L9 13.125l.423 1.059a.956.956 0 01-1.652.928l-.679-.906a1.125 1.125 0 00-1.906.172L4.5 15.75l-.612.153M12.75 3.031a9 9 0 00-8.862 12.872M12.75 3.031a9 9 0 016.69 14.036m0 0l-.177-.529A2.25 2.25 0 0017.128 15H16.5l-.324-.324a1.453 1.453 0 00-2.328.377l-.036.073a1.586 1.586 0 01-.982.816l-.99.282c-.55.157-.894.702-.8 1.267l.073.438c.08.474.49.821.97.821.846 0 1.598.542 1.865 1.345l.215.643m5.276-3.67a9.012 9.012 0 01-5.276 3.67m0 0a9 9 0 01-10.275-4.835M15.75 9c0 .896-.393 1.7-1.016 2.25"/>
</svg>

export default CouponURLsPanel;