<?php
/** Strict read-only projections for OceanWP 4.2.2 free settings. */

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

define( 'WPAGENT_OCEANWP_FREE_SURFACE_THEME_VERSION', '4.2.2' );
define( 'WPAGENT_OCEANWP_FREE_SURFACE_MAX_HOOKS', 10 );
define( 'WPAGENT_OCEANWP_FREE_SURFACE_MAX_HOOK_BYTES', 2048 );

/** @return true|WP_Error */
function wpagent_oceanwp_free_surface_require_theme() {
	if ( ! wpagent_oceanwp_is_active() ) {
		return new WP_Error( 'wpagent_oceanwp_inactive', __( 'OceanWP oder ein OceanWP-Child-Theme ist nicht aktiv.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	if ( WPAGENT_OCEANWP_FREE_SURFACE_THEME_VERSION !== wpagent_oceanwp_version() || ! wpagent_oceanwp_runtime_ready() ) {
		return new WP_Error( 'wpagent_oceanwp_free_surface_runtime_missing', __( 'Diese OceanWP-Leseoberfläche erfordert genau OceanWP 4.2.2.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	return true;
}

/** @return true|WP_Error */
function wpagent_oceanwp_free_surface_require_extra() {
	$theme = wpagent_oceanwp_free_surface_require_theme();
	if ( is_wp_error( $theme ) ) {
		return $theme;
	}
	if ( ! class_exists( 'Ocean_Extra' ) || WPAGENT_OCEAN_EXTRA_MIN_VERSION !== wpagent_oceanwp_extra_version() ) {
		return new WP_Error( 'wpagent_oceanwp_free_surface_extra_runtime_missing', __( 'Die OceanWP-Hook- und Beitragsoberfläche erfordert genau Ocean Extra 2.5.8.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	return true;
}

/** @return array{blog_id:int,version:string,mods:array<string,mixed>}|WP_Error */
function wpagent_oceanwp_free_surface_theme_snapshot() {
	$ready = wpagent_oceanwp_free_surface_require_theme();
	if ( is_wp_error( $ready ) ) {
		return $ready;
	}
	$blog_id = (int) get_current_blog_id();
	if ( $blog_id < 1 ) {
		return new WP_Error( 'wpagent_oceanwp_free_surface_invalid_site', __( 'Die OceanWP-Websitezuordnung ist ungültig.', 'wp-agent-companion' ), array( 'status' => 502 ) );
	}
	$snapshot = wpagent_oceanwp_snapshot();
	if ( is_wp_error( $snapshot ) ) {
		return $snapshot;
	}
	return array( 'blog_id' => $blog_id, 'version' => wpagent_oceanwp_version(), 'mods' => $snapshot['mods']['value'] );
}

/** @param array<string,mixed> $state */
function wpagent_oceanwp_free_surface_hash( string $scope, int $blog_id, array $state ): string {
	return hash( 'sha256', $blog_id . "\0" . WPAGENT_OCEANWP_FREE_SURFACE_THEME_VERSION . "\0" . $scope . "\0" . maybe_serialize( $state ) );
}

/** @return mixed */
function wpagent_oceanwp_free_surface_mod( array $mods, string $key, $default ) {
	return array_key_exists( $key, $mods ) ? $mods[ $key ] : $default;
}

/** @return bool|WP_Error */
function wpagent_oceanwp_free_surface_bool( $value ) {
	if ( is_bool( $value ) ) {
		return $value;
	}
	if ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
		return 1 === $value;
	}
	if ( is_string( $value ) && ( '0' === $value || '1' === $value ) ) {
		return '1' === $value;
	}
	return wpagent_oceanwp_free_surface_invalid_state();
}

/** @param string[] $allowed @return string|WP_Error */
function wpagent_oceanwp_free_surface_enum( $value, array $allowed ) {
	if ( ! is_string( $value ) || ! in_array( $value, $allowed, true ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	return $value;
}

/** @return int|WP_Error */
function wpagent_oceanwp_free_surface_int( $value, int $minimum, int $maximum ) {
	if ( is_int( $value ) ) {
		$number = $value;
	} elseif ( is_string( $value ) && ctype_digit( $value ) ) {
		$number = (int) $value;
	} else {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	return $number >= $minimum && $number <= $maximum ? $number : wpagent_oceanwp_free_surface_invalid_state();
}

/** @return string|WP_Error */
function wpagent_oceanwp_free_surface_text( $value, int $maximum ) {
	if ( ! is_string( $value ) || strlen( $value ) > $maximum || wp_check_invalid_utf8( $value ) !== $value || preg_match( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', $value ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	return $value;
}

function wpagent_oceanwp_free_surface_invalid_state(): WP_Error {
	return new WP_Error( 'wpagent_oceanwp_free_surface_invalid_state', __( 'OceanWP enthält einen nicht sicher auswertbaren freien Einstellungsstand.', 'wp-agent-companion' ), array( 'status' => 502 ) );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_oceanwp_free_surface_footer( array $mods ) {
	$settings = array(
		'widgets_enabled'    => wpagent_oceanwp_free_surface_bool( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_widgets', true ) ),
		'widgets_visibility' => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_widgets_visibility', 'all-devices' ), array( 'all-devices', 'hide-tablet', 'hide-mobile', 'hide-tablet-mobile' ) ),
		'widgets_columns'    => array(
			'desktop' => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_widgets_columns', 4 ), 1, 4 ),
			'tablet'  => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_widgets_tablet_columns', 2 ), 1, 4 ),
			'mobile'  => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_widgets_mobile_columns', 1 ), 1, 4 ),
		),
		'bottom_enabled'     => wpagent_oceanwp_free_surface_bool( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_bottom', true ) ),
		'bottom_visibility'  => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_bottom_footer_visibility', 'all-devices' ), array( 'all-devices', 'hide-tablet', 'hide-mobile', 'hide-tablet-mobile' ) ),
		'copyright_text'     => wpagent_oceanwp_free_surface_text( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_footer_copyright_text', 'Copyright [oceanwp_date] - WordPress Theme by OceanWP' ), WPAGENT_OCEANWP_FREE_SURFACE_MAX_HOOK_BYTES ),
	);
	foreach ( $settings as $value ) {
		if ( is_wp_error( $value ) || ( is_array( $value ) && array_filter( $value, 'is_wp_error' ) ) ) {
			return wpagent_oceanwp_free_surface_invalid_state();
		}
	}
	return $settings;
}

/** @return array<string,mixed>|WP_Error */
function wpagent_oceanwp_free_surface_blog( array $mods ) {
	$layouts  = array( 'right-sidebar', 'left-sidebar', 'full-width', 'full-screen', 'both-sidebars' );
	$settings = array(
		'archive_layout'       => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_archives_layout', 'right-sidebar' ), $layouts ),
		'archive_style'        => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_style', 'large-entry' ), array( 'large-entry', 'grid-entry', 'thumbnail-entry' ) ),
		'grid_columns'         => array(
			'desktop' => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_grid_columns', 2 ), 1, 6 ),
			'tablet'  => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_grid_columns_tablet', 2 ), 1, 6 ),
			'mobile'  => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_grid_columns_mobile', 1 ), 1, 6 ),
		),
		'excerpt_length'       => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_entry_excerpt_length', 30 ), 1, 500 ),
		'pagination_style'     => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_pagination_style', 'standard' ), array( 'standard', 'infinite_scroll', 'load_more', 'next_prev' ) ),
		'single_layout'        => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_single_layout', 'right-sidebar' ), $layouts ),
		'single_content_width' => wpagent_oceanwp_free_surface_int( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_blog_single_content_width', 700 ), 0, 2000 ),
	);
	foreach ( $settings as $value ) {
		if ( is_wp_error( $value ) || ( is_array( $value ) && array_filter( $value, 'is_wp_error' ) ) ) {
			return wpagent_oceanwp_free_surface_invalid_state();
		}
	}
	return $settings;
}

/** @return array<string,mixed>|WP_Error */
function wpagent_oceanwp_free_surface_woocommerce( array $mods ) {
	if ( ! class_exists( 'WooCommerce' ) ) {
		return array( 'available' => false, 'settings' => (object) array() );
	}
	$layouts  = array( 'right-sidebar', 'left-sidebar', 'full-width', 'full-screen', 'both-sidebars' );
	$settings = array(
		'shop_layout'          => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_shop_layout', 'left-sidebar' ), $layouts ),
		'product_style'        => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_products_style', 'default' ), array( 'default', 'hover' ) ),
		'grid_list_enabled'    => wpagent_oceanwp_free_surface_bool( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_grid_list', true ) ),
		'result_count_enabled' => wpagent_oceanwp_free_surface_bool( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_shop_result_count', true ) ),
		'pagination_style'     => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_pagination_style', 'standard' ), array( 'standard', 'infinite_scroll', 'load_more' ) ),
		'product_layout'       => wpagent_oceanwp_free_surface_enum( wpagent_oceanwp_free_surface_mod( $mods, 'ocean_woo_product_layout', 'left-sidebar' ), $layouts ),
	);
	foreach ( $settings as $value ) {
		if ( is_wp_error( $value ) ) {
			return $value;
		}
	}
	return array( 'available' => true, 'settings' => $settings );
}

/** @return list<array{id:string,action:string,area:string,meta:string}> */
function wpagent_oceanwp_free_surface_hooks(): array {
	return array(
		array( 'id' => 'before_top_bar', 'action' => 'ocean_before_top_bar', 'area' => 'top_bar', 'meta' => 'ocean_shortcode_before_top_bar' ),
		array( 'id' => 'after_top_bar', 'action' => 'ocean_after_top_bar', 'area' => 'top_bar', 'meta' => 'ocean_shortcode_after_top_bar' ),
		array( 'id' => 'before_header', 'action' => 'ocean_before_header', 'area' => 'header', 'meta' => 'ocean_shortcode_before_header' ),
		array( 'id' => 'after_header', 'action' => 'ocean_after_header', 'area' => 'header', 'meta' => 'ocean_shortcode_after_header' ),
		array( 'id' => 'before_title', 'action' => 'ocean_before_page_header', 'area' => 'page_header', 'meta' => 'ocean_has_shortcode' ),
		array( 'id' => 'after_title', 'action' => 'ocean_after_page_header', 'area' => 'page_header', 'meta' => 'ocean_shortcode_after_title' ),
		array( 'id' => 'before_footer_widgets', 'action' => 'ocean_before_footer_widgets', 'area' => 'footer_widgets', 'meta' => 'ocean_shortcode_before_footer_widgets' ),
		array( 'id' => 'after_footer_widgets', 'action' => 'ocean_after_footer_widgets', 'area' => 'footer_widgets', 'meta' => 'ocean_shortcode_after_footer_widgets' ),
		array( 'id' => 'before_footer_bottom', 'action' => 'ocean_before_footer_bottom', 'area' => 'footer_bottom', 'meta' => 'ocean_shortcode_before_footer_bottom' ),
		array( 'id' => 'after_footer_bottom', 'action' => 'ocean_after_footer_bottom', 'area' => 'footer_bottom', 'meta' => 'ocean_shortcode_after_footer_bottom' ),
	);
}

/** @return array{post:WP_Post,blog_id:int}|WP_Error */
function wpagent_oceanwp_free_surface_post_target( int $post_id ) {
	$ready = wpagent_oceanwp_free_surface_require_extra();
	if ( is_wp_error( $ready ) ) {
		return $ready;
	}
	return wpagent_oceanwp_extra_layout_post_target( $post_id );
}

/** @return array{exists:bool,value:string}|WP_Error */
function wpagent_oceanwp_free_surface_post_meta( int $post_id, string $key ) {
	$raw  = get_metadata_raw( 'post', $post_id, $key, false );
	$rows = null === $raw ? array() : ( is_array( $raw ) ? $raw : array( $raw ) );
	if ( count( $rows ) > 1 || ( 1 === count( $rows ) && ! is_string( $rows[0] ) ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	$value = 1 === count( $rows ) ? $rows[0] : '';
	$text  = wpagent_oceanwp_free_surface_text( $value, WPAGENT_OCEANWP_FREE_SURFACE_MAX_HOOK_BYTES );
	return is_wp_error( $text ) ? $text : array( 'exists' => 1 === count( $rows ), 'value' => $text );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_oceanwp_free_surface_post_snapshot( WP_Post $post, int $blog_id ) {
	if ( $blog_id < 1 || $blog_id !== (int) get_current_blog_id() || ! current_user_can( 'edit_post', $post->ID ) ) {
		return new WP_Error( 'wpagent_oceanwp_free_surface_post_changed', __( 'Der OceanWP-Beitrag kann nicht mehr sicher gelesen werden.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	$meta = array();
	foreach ( array( 'ocean_second_sidebar', 'ocean_both_sidebars_sidebars_width', 'ocean_disable_margins', 'ocean_display_top_bar' ) as $key ) {
		$value = wpagent_oceanwp_free_surface_post_meta( $post->ID, $key );
		if ( is_wp_error( $value ) ) {
			return $value;
		}
		$meta[ $key ] = $value;
	}
	foreach ( wpagent_oceanwp_free_surface_hooks() as $hook ) {
		$value = wpagent_oceanwp_free_surface_post_meta( $post->ID, $hook['meta'] );
		if ( is_wp_error( $value ) ) {
			return $value;
		}
		$meta[ $hook['meta'] ] = $value;
	}
	$sidebar = $meta['ocean_second_sidebar']['value'];
	if ( '' !== $sidebar && ! wpagent_oceanwp_extra_layout_sidebar_is_safe( $sidebar ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	$width = $meta['ocean_both_sidebars_sidebars_width']['value'];
	if ( '' !== $width && is_wp_error( wpagent_oceanwp_free_surface_int( $width, 0, 100 ) ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	if ( ! in_array( $meta['ocean_disable_margins']['value'], array( '', 'enable', 'on' ), true ) || ! in_array( $meta['ocean_display_top_bar']['value'], array( '', 'default', 'on', 'off' ), true ) ) {
		return wpagent_oceanwp_free_surface_invalid_state();
	}
	return array( 'blog_id' => $blog_id, 'post_id' => $post->ID, 'post_type' => $post->post_type, 'meta' => $meta );
}

/** @param array<string,mixed> $snapshot */
function wpagent_oceanwp_free_surface_post_hash( array $snapshot ): string {
	return hash( 'sha256', $snapshot['blog_id'] . "\0" . WPAGENT_OCEANWP_FREE_SURFACE_THEME_VERSION . "\0" . WPAGENT_OCEAN_EXTRA_MIN_VERSION . "\0" . $snapshot['post_id'] . "\0" . $snapshot['post_type'] . "\0" . maybe_serialize( $snapshot['meta'] ) );
}

/** @param array<string,mixed> $snapshot @return array<string,mixed> */
function wpagent_oceanwp_free_surface_post_normalize( array $snapshot ): array {
	$meta    = $snapshot['meta'];
	$sidebar = $meta['ocean_second_sidebar']['value'];
	$width   = $meta['ocean_both_sidebars_sidebars_width']['value'];
	$hooks   = array();
	foreach ( wpagent_oceanwp_free_surface_hooks() as $hook ) {
		$hooks[ $hook['id'] ] = '' !== $meta[ $hook['meta'] ]['value'];
	}
	return array(
		'layout' => array(
			'second_sidebar' => '' === $sidebar ? array( 'mode' => 'inherit' ) : array( 'mode' => 'set', 'sidebar_id' => $sidebar ),
			'sidebars_width' => '' === $width || 0 === (int) $width ? array( 'mode' => 'inherit' ) : array( 'mode' => 'set', 'value' => (int) $width ),
		),
		'visibility' => array(
			'margins' => 'on' === $meta['ocean_disable_margins']['value'] ? 'removed' : ( '' === $meta['ocean_disable_margins']['value'] ? 'inherit' : 'normal' ),
			'top_bar' => 'on' === $meta['ocean_display_top_bar']['value'] ? 'show' : ( 'off' === $meta['ocean_display_top_bar']['value'] ? 'hide' : 'inherit' ),
		),
		'configured_hooks' => $hooks,
	);
}
