<?php

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

namespace Resursbank\MerchantApi\Hook;

use Context;
use Resursbank\Ecom\Module\Customer\Widget\GetAddress;
use Resursbank\MerchantApi\Util\Log;
use Throwable;

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

        return '';
    }
}
