<?php

namespace MDCE;

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

class Settings {

	private const OPTION_KEY = 'mdce_settings';

	private array $defaults = [
		'post_types'     => [ 'post', 'page' ],
		'default_action' => 'draft',
	];

	private array $options;

	public function __construct() {
		$stored        = get_option( self::OPTION_KEY, [] );
		$this->options = wp_parse_args( is_array( $stored ) ? $stored : [], $this->defaults );
	}

	public function get( string $key ): mixed {
		return $this->options[ $key ] ?? null;
	}

	public function save( array $raw ): void {
		$sanitized = [
			'post_types'     => array_values( array_filter( array_map( 'sanitize_key', (array) ( $raw['post_types'] ?? [] ) ) ) ),
			'default_action' => sanitize_key( $raw['default_action'] ?? 'draft' ),
		];

		update_option( self::OPTION_KEY, $sanitized );
		$this->options = wp_parse_args( $sanitized, $this->defaults );
	}
}
