<?php

/**
 * Helper functions for WP DB.
 *
 * @package northmill-online-shared
 * @noinspection PhpUnused
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstract_Shared_Base;
defined('ABSPATH') || exit;
/**
 * Class DB_Helpers
 */
class Hook_Helpers
{
    /**
     * Apply a filter twice. Once with the hook name, and once with the hook name concatenated with the service name.
     *
     * @param string $hook The hook name.
     * @param mixed  $value The value to filter.
     * @param mixed  ...$args The arguments to pass to the filter.
     *
     * @return mixed
     */
    public static function apply_scoped_filters(string $hook, mixed $value, ...$args): mixed
    {
        $scope = Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored();
        return self::apply_scoped_filters_custom($scope, $hook, $value, ...$args);
    }
    /**
     * Apply a filter twice. Once with the hook name, and once with the hook name concatenated with the scope.
     *
     * @param string $scope The scope name.
     * @param string $hook The hook name.
     * @param mixed  $value The value to filter.
     * @param mixed  ...$args The arguments to pass to the filter.
     *
     * @return mixed
     */
    public static function apply_scoped_filters_custom(string $scope, string $hook, mixed $value, ...$args): mixed
    {
        $scoped_hook = $hook . '_' . $scope;
        $value = apply_filters($hook, $value, ...$args);
        return apply_filters($scoped_hook, $value, ...$args);
    }
    /**
     * Add a filter with this Shared instance as scope.
     *
     * @param string   $hook The hook name.
     * @param callable $callback The callback function.
     * @param int      $priority The priority.
     * @param int      $accepted_args The number of arguments the function accepts.
     *
     * @return void
     */
    public static function add_scoped_filter(string $hook, callable $callback, int $priority = 10, int $accepted_args = 1): void
    {
        self::add_scoped_filter_custom(Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored(), $hook, $callback, $priority, $accepted_args);
    }
    /**
     * Add a filter with a scope.
     *
     * @param string   $scope The scope name.
     * @param string   $hook The hook name.
     * @param callable $callback The callback function.
     * @param int      $priority The priority.
     * @param int      $accepted_args The number of arguments the function accepts.
     *
     * @return void
     */
    public static function add_scoped_filter_custom(string $scope, string $hook, callable $callback, int $priority = 10, int $accepted_args = 1): void
    {
        $scoped_hook = $hook . '_' . $scope;
        add_filter($scoped_hook, $callback, $priority, $accepted_args);
    }
    /**
     * Do an action twice. Once with the hook name, and once with the hook name concatenated with the service name.
     *
     * @param string $hook The hook name.
     * @param mixed  ...$args The arguments to pass to the action.
     *
     * @return void
     */
    public static function do_scoped_action(string $hook, ...$args): void
    {
        self::do_scoped_action_custom(Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored(), $hook, ...$args);
    }
    /**
     * Do an action twice. Once with the hook name, and once with the hook name concatenated with the scope.
     *
     * @param string $scope The scope name.
     * @param string $hook The hook name.
     * @param mixed  ...$args The arguments to pass to the action.
     *
     * @return void
     */
    public static function do_scoped_action_custom(string $scope, string $hook, ...$args): void
    {
        $scoped_hook = $hook . '_' . $scope;
        do_action($hook, ...$args);
        do_action($scoped_hook, ...$args);
    }
    /**
     * Helper to add an action with this Shared instance as scope.
     *
     * @param string   $hook The hook name.
     * @param callable $callback The callback function.
     * @param int      $priority The priority.
     * @param int      $accepted_args The number of arguments the function accepts.
     *
     * @return void
     *
     * @used Northmill Sync
     */
    public static function add_scoped_action(string $hook, callable $callback, int $priority = 10, int $accepted_args = 1): void
    {
        self::add_scoped_action_custom(Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored(), $hook, $callback, $priority, $accepted_args);
    }
    /**
     * Helper to add an action with a scope.
     *
     * @param string   $scope The scope name.
     * @param string   $hook The hook name.
     * @param callable $callback The callback function.
     * @param int      $priority The priority.
     * @param int      $accepted_args The number of arguments the function accepts.
     *
     * @return void
     */
    public static function add_scoped_action_custom(string $scope, string $hook, callable $callback, int $priority = 10, int $accepted_args = 1): void
    {
        $scoped_hook = $hook . '_' . $scope;
        add_action($scoped_hook, $callback, $priority, $accepted_args);
    }
}
