<?php
namespace PortalPeople\DisabledPostcodes\Model\Checkout;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Store\Model\StoreManagerInterface;
use PortalPeople\DisabledPostcodes\Model\Config;
use PortalPeople\DisabledPostcodes\Model\PostcodeMatcher;

class DisabledPostcodesConfigProvider implements ConfigProviderInterface
{
    private StoreManagerInterface $storeManager;
    private PostcodeMatcher $matcher;
    private Config $config;

    public function __construct(
        StoreManagerInterface $storeManager,
        PostcodeMatcher $matcher,
        Config $config
    ) {
        $this->storeManager = $storeManager;
        $this->matcher = $matcher;
        $this->config = $config;
    }

    public function getConfig(): array
    {
        $storeId = (int)$this->storeManager->getStore()->getId();
        $rules = $this->matcher->getRulesByStore($storeId);

        return [
            'portalpeopleDisabledPostcodes' => [
                'enabled' => $this->config->isEnabled($storeId),
                'message' => $this->config->getBlockedMessage($storeId),
                'exact' => $rules['exact'],
                'areas' => $rules['areas'],
            ],
        ];
    }
}
