<?php

namespace Resursbank\Ecom\Module\Widget\PaymentInformation;

use Resursbank\Ecom\Exception\FilesystemException;
use Resursbank\Ecom\Lib\Widget\Widget;

/**
 * JavaScript to reload the payment information widget on demand.
 */
class Js extends Widget
{
    /**
     * @param float $amount | Current order amount when entering view.
     * @param string $reloadUrl | URL to fetch new widget HTML from.
     * @param string $amountElement | DOM path to amount element.
     * @param array $observableElements | List of DOM paths to trigger reload on.
     * @param bool $automatic | Whether to initiate JS automatically.
     * @throws FilesystemException
     */
    public function __construct(
        public readonly float $amount,
        public readonly string $reloadUrl,
        public readonly string $widgetElement,
        public readonly string $amountElement,
        public readonly array $observableElements,
        public readonly bool $automatic = true
    ) {
        $this->content = $this->render(file: __DIR__ . '/templates/js.js.phtml');
    }
}
