<?php
/**
 * Optional feature modules (enabled/disabled from the dashboard).
 *
 * @package CraftAddonsForWPBakery
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class CAFW_Modules
 */
class CAFW_Modules {

	const OPTION = 'cafw_disabled_modules';

	/**
	 * @return array<string,array{label:string,description:string,icon:string}>
	 */
	public static function all() {
		return apply_filters(
			'cafw_modules',
			array(
				'responsive-visibility' => array(
					'label'       => __( 'Responsive Visibility', 'craft-addons-for-wpbakery' ),
					'description' => __( 'Hide elements per screen size.', 'craft-addons-for-wpbakery' ),
					'icon'        => 'dashicons-visibility',
				),
				'dynamic-content'       => array(
					'label'       => __( 'Dynamic Content', 'craft-addons-for-wpbakery' ),
					'description' => __( 'Insert live post & field data.', 'craft-addons-for-wpbakery' ),
					'icon'        => 'dashicons-database',
				),
			)
		);
	}

	/**
	 * Pro module metadata, shown as locked teasers in the free build where the
	 * /pro folder is stripped.
	 *
	 * @return array<string,array{label:string,description:string,icon:string}>
	 */
	public static function pro_catalog() {
		return array(
			'scroll-animations' => array(
				'label'       => __( 'Scroll Animations', 'craft-addons-for-wpbakery' ),
				'description' => __( 'Animate elements on scroll.', 'craft-addons-for-wpbakery' ),
				'icon'        => 'dashicons-update',
			),
		);
	}

	/**
	 * @return string[]
	 */
	public static function get_disabled() {
		$disabled = get_option( self::OPTION, array() );
		return is_array( $disabled ) ? $disabled : array();
	}

	/**
	 * @param string $key Module key.
	 * @return bool
	 */
	public static function is_enabled( $key ) {
		return ! in_array( $key, self::get_disabled(), true );
	}

	/**
	 * @param string[] $enabled Enabled module keys.
	 * @param string[] $all     All module keys.
	 * @return void
	 */
	public static function save_enabled( array $enabled, array $all ) {
		update_option( self::OPTION, array_values( array_diff( $all, $enabled ) ) );
	}
}
