<?php

namespace WPDRMS\ASP\Priorities\ORM;

use WPDRMS\AdminUI\Data\AbstractOptionDataSiteOption;
use WPDRMS\AdminUI\Options\PriorityGroupsArrayOption;
use WPDRMS\AdminUI\Options\PriorityGroupsOption;

if ( ! defined( 'ABSPATH' ) ) die( '-1' );

final class PriorityGroupsOptions extends AbstractOptionDataSiteOption {

	public PriorityGroupsArrayOption $groups;

	protected const OPTION_NAME = 'asp_priority_groups';
	protected const OPTIONS     = array(
		'groups' => array(
			'type'         => 'priority_groups_array',
			'default_args' => array( 'groups' => array() ),
		),
	);

	/**
	 * @return array<int, array<string, mixed>>
	 */
	public function getGroups(): array {
		return array_map( static fn( $g ) => $g->jsonSerialize(), $this->groups->groups );
	}

	/**
	 * @param array<int, array<string, mixed>> $rawGroups
	 */
	public function saveGroups( array $rawGroups ): self {
		$this->groups->groups = array_map(
			static fn( $g ) => new PriorityGroupsOption( is_array( $g ) ? $g : (array) $g ),
			$rawGroups
		);
		return parent::save();
	}
}
