<?php

namespace $NAMESPACE$;

use Modules\Core\Facades\Innoclapps;
use Illuminate\Support\ServiceProvider;

class $CLASS$ extends ServiceProvider
{
    protected string $moduleName = '$MODULE$';

    protected string $moduleNameLower = '$LOWER_NAME$';

    /**
     * Boot the application events.
     */
    public function boot() : void
    {
        $this->registerTranslations();
        $this->registerConfig();
        $this->registerViews();
        $this->loadMigrationsFrom(module_path($this->moduleName, '$MIGRATIONS_PATH$'));

        $this->app->booted($this->bootModule(...));
    }

    /**
     * Register the service providers.
     */
    public function register() : void
    {
        $this->app->register(RouteServiceProvider::class);
    }

    /**
     * Register config.
     */
    protected function registerConfig() : void
    {
        $this->publishes([
            module_path($this->moduleName, '$PATH_CONFIG$/config.php') => config_path($this->moduleNameLower . '.php'),
        ], 'config');

        $this->mergeConfigFrom(
            module_path($this->moduleName, '$PATH_CONFIG$/config.php'), $this->moduleNameLower
        );
    }

    /**
     * Register views.
     */
    public function registerViews() : void
    {
        $viewPath = resource_path('views/modules/' . $this->moduleNameLower);

        $sourcePath = module_path($this->moduleName, '$PATH_VIEWS$');

        $this->publishes([
            $sourcePath => $viewPath
        ], ['views', $this->moduleNameLower . '-module-views']);

        $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
    }

    /**
     * Register translations.
     */
    public function registerTranslations() : void
    {
        $this->loadTranslationsFrom(module_path($this->moduleName, '$PATH_LANG$'), $this->moduleNameLower);
    }

    /**
     * Boot the module.
     */
    protected function bootModule() : void
    {
        Innoclapps::whenReadyForServing(function() {
            Innoclapps::booted($this->shareDataToScript(...));
        });

        Innoclapps::vite(
            $this->moduleNameLower,
            [
                'resources/js/app.js',
                // 'resources/css/app.css',
            ],
            'build-'.$this->moduleNameLower
        );
    }

    /**
     * Share data to script.
     */
    protected function shareDataToScript() : void
    {
        Innoclapps::provideToScript([
            '$LOWER_NAME$' => []
        ]);
    }

    /**
     * Get the services provided by the provider.
     */
    public function provides() : array
    {
        return [];
    }

    /**
     * Get the publishable view paths.
     */
    private function getPublishableViewPaths() : array
    {
        $paths = [];

        foreach ($this->app['config']->get('view.paths') as $path) {
            if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
                $paths[] = $path . '/modules/' . $this->moduleNameLower;
            }
        }

        return $paths;
    }
}
