<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$version = str_replace("-dev", "", (string)$objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class)->getVersion());
$isMage23 = version_compare($version, '2.3.0', '>=');
$isMage244 = $isMage23 && version_compare($version, '2.4.4', '>=');
$mediaUrl = $this->helper('Codazon\Shopbybrandpro\Helper\Wysiwyg\Images')->getBaseUrl();
$defaultThumbnail = $objectManager->get(\Magento\Framework\View\Asset\Repository::class)->getUrl('Codazon_Shopbybrandpro/images/placeholder_thumbnail.jpg');
?>
<?php if ((!$isMage244) && $isMage23) : ?>
<script>
require.config({
    map: {
        '*': {
            tinymce: 'tiny_mce_4/tinymce.min'
        }
    }
});
</script>
<?php endif ?>
<script>
if (typeof Codazon == 'undefined') {
    var Codazon = {};
}
Codazon.mediaUrl = '<?php echo $mediaUrl; ?>';
require(['jquery', 'Codazon_Shopbybrandpro/js/browser'], function($) {
    var mediaUrl = Codazon.mediaUrl;
    var defaultThumbnail = '<?php echo $defaultThumbnail ?>';
    var changePreviewImage = function(obj) {
        var $_input = $(obj);
        if ($_input.parent().find('.attached_image img').length > 0) {
            var $_img = $_input.parent().find('.attached_image img').first();
            $_img.attr('src', mediaUrl + $_input.val());
        }
        $_input.prop('disabled', false);
    };
    window.changePreviewImage = changePreviewImage;
    $('body').on('change', '.js-change-preview', function() {
        changePreviewImage(this);
    });
    window.clearValue = function(obj) {
        $_btn = $(obj);
        if ($_btn.parent().find('.input-image').length > 0) {
            var $_input = $_btn.parent().find('.input-image').first();
            $_input.val('');
        }
        if ($_btn.parent().find('.attached_image img').length > 0) {
            var $_img = $_btn.parent().find('.attached_image img').first();
            $_img.attr('src', defaultThumbnail);
        }
    }
});
</script>