<?php

namespace CommerceBird\Admin\Actions\Ajax;

use CommerceBird\Admin\Traits\AjaxRequest;
use CommerceBird\Admin\Traits\OptionStatus;
use CommerceBird\Admin\Traits\Singleton;
use CommerceBird\Admin\Traits\LogWriter;


if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Initializes the ACF functions class.
 *
 * @since 1.0.0
 * @return void
 */
final class AcfAjax {

	use Singleton;
	use AjaxRequest;
	use OptionStatus;
	use LogWriter;



	private const ACTIONS = array(

		'get_acf_fields' => 'get_acf_fields',
	);

	public function __construct() {
		if ( ! class_exists( 'ACF' ) || ! function_exists( 'acf_get_field_groups' ) || ! function_exists( 'acf_get_fields' ) ) {
			return;
		}

		$this->load_actions();
	}


	public function get_acf_fields(): void {
		if ( ! function_exists( 'acf_get_field_groups' ) || ! function_exists( 'acf_get_fields' ) ) {
			$this->response['fields'] = array();
			$this->serve();
			return;
		}
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin-only AJAX call, nonce verified in parent.
		$post_type_value = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : '';
		if ( 'users' === $post_type_value ) {
			$post_type_value = 'all';
			$group_type      = 'user_role';
		} else {
			$group_type = 'post_type';
		}
		// Get all field groups associated with WooCommerce products.
		$groups = acf_get_field_groups( array( $group_type => $post_type_value ) );

		// Check if there are any field groups.
		if ( ! $groups ) {
			$this->response['fields'] = array();
			$this->serve();
			return;
		}

		$fields = array();
		foreach ( $groups as $group ) {
			$group_fields = acf_get_fields( $group['key'] );
			if ( is_array( $group_fields ) ) {
				$fields = array_merge( $fields, $group_fields );
			}
		}

		$this->response['fields'] = $fields;
		$this->serve();
	}
}
