<?php
/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        https://github.com/foliolabs/docman for the canonical source repository
 */

namespace Foliolabs\Docman;

use Foliolabs\WP;

class BlockScriptCommon
{
    public static function addCommonScripts()
    {
        static $enqueued = false;

        if (!$enqueued)
        {
            \Foliokit::getObject('translator')->load('com:docman');

            $view = \Foliokit::getObject('com:docman.view.default.html');
            $template = $view->getTemplate()
                ->addFilter('style')
                ->addFilter('script');

            $string = "
            <?= helper('behavior.foliokit'); ?>
            <?= helper('behavior.select2'); ?>
            <?= helper('behavior.autocomplete', [
                'options' => [
                    'url' => 'https://example.com/' // required option
                ]
            ]); ?>
            <?= helper('translator.script', [
                'strings' => [
                    'Docman menu documents'
                ]
            ]); ?>
            ";

            $template->render($string, [
            ], 'php');

            WP::wp_enqueue_script('foliokit-blocks-shared-js', WP::getPluginUrl().'/foliolabs-docman/base/resources/assets/js/block/common.js', [
                'wp-dom-ready',
                'wp-blocks',
                'wp-i18n',
                'wp-editor',
                'wp-element',
                'wp-components',]);

            $enqueued = true;
        }

    }
}