<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Framework\Escaper;
use Magento\Framework\View\Helper\SecureHtmlRenderer;
use Magento\Sales\Block\Adminhtml\Order\Create\Sidebar;
use Magento\Sales\ViewModel\Order\Create\SidebarPermissionCheck;

/**
 * @var Sidebar $block
 * @var SecureHtmlRenderer $secureRenderer
 * @var Escaper $escaper
 */

/**
 * @var SidebarPermissionCheck $sideBarPermissionCheck
 */
$sideBarPermissionCheck = $block->getData('sideBarPermissionCheck');

?>
<?php  if ($sideBarPermissionCheck->isAllowed()): ?>
<div class="customer-current-activity-inner">
    <h4 class="customer-activity-title"><?= $escaper->escapeHtml(__('Customer\'s Activities')) ?></h4>
    <div class="create-order-sidebar-container">
    <?= $block->getChildHtml('top_button') ?>
    <?php foreach ($block->getLayout()->getChildBlocks($block->getNameInLayout()) as $_alias => $_child): ?>
        <?php if ($_alias != 'top_button' && $_alias != 'bottom_button'): ?>
            <?php if ($block->canDisplay($_child)): ?>
                <div class="order-sidebar-block" id="order-sidebar_<?= $escaper->escapeHtmlAttr($_alias) ?>">
                    <?= $block->getChildHtml($_alias) ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    <?php endforeach; ?>
    <?= $block->getChildHtml('bottom_button') ?>
    </div>
</div>
<?php endif; ?>
<?php $scriptString = <<<script
require([
    "prototype",
    "Magento_Catalog/catalog/product/composite/configure"
], function(){

    function addSidebarCompositeListType() {
        productConfigure.addListType('sidebar', {
            urlFetch: '{$block->escapeJs($block->getUrl('sales/order_create/configureProductToAdd'))}',
            urlConfirm: '{$block->escapeJs($block->getUrl('sales/order_create/addConfigured'))}'
        });
        productConfigure.addListType('sidebar_wishlist', {
            urlFetch: '{$block->escapeJs($block->getUrl('customer/wishlist_product_composite_wishlist/configure'))}',
            urlConfirm: '{$block->escapeJs($block->getUrl('sales/order_create/addConfigured'))}'
        });
    }

    if (typeof(sidebarListTypeWorkedOn) == 'undefined') {
        sidebarListTypeWorkedOn = false;
    }
    if (!sidebarListTypeWorkedOn) {
        if (typeof(productConfigure) == 'undefined') {
            Event.observe(window, 'load',  function(){
                setTimeout(addSidebarCompositeListType, 10);
            })
        } else {
            addSidebarCompositeListType();
        }
        sidebarListTypeWorkedOn = true;
    }

    window.addSidebarCompositeListType = addSidebarCompositeListType;
});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
