<?php

/**
 * Copyright © Resurs Bank AB. All rights reserved.
 * See LICENSE for license details.
 */

namespace Resursbank\MerchantApi\Hook;

use Context;
use Resursbank\Ecom\Module\Widget\GetAddress\Html as GetAddress;
use Resursbank\Ecom\Module\Widget\GetAddress\Js as GetAddressJs;
use Resursbank\Ecom\Module\Widget\GetAddress\Css as GetAddressCss;
use Resursbank\MerchantApi\Util\Log;
use Throwable;

/**
 * Add fetch address widget to check out page.
 */
class DisplayAddressSelectorBottom
{
    /**
     * @return string
     */
    public static function exec(): string
    {
        try {
            $getAddressJs = new GetAddressJs(
                url: Context::getContext()->link->getModuleLink(
                    module: 'resursbank',
                    controller: 'fetchaddress'
                ),
            );
            $getAddressCss = new GetAddressCss();
            $getAddressWidget = new GetAddress();
            return "<script>$getAddressJs->content</script><style>$getAddressCss->content</style><div id='rb-ga-widget-container' style='display:none;'>$getAddressWidget->content</div>";
        } catch (Throwable $error) {
            Log::error($error);
        }

        return '';
    }
}
