<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * Update Subscriber Action Definition
 *
 * @package Sequensy\Integrations\CampaignMonitor
 * @since 1.0.0
 */

declare(strict_types=1);

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

return array(
	'type'            => 'update_subscriber',
	'label'           => __( 'Update Subscriber', 'sequensy' ),
	'description'     => __( 'Update an existing subscriber on a Campaign Monitor list.', 'sequensy' ),
	'category'        => 'subscriber',
	'required_fields' => array( 'list_id', 'email' ),
	'schema'          => array(
		array(
			'name'        => 'list_id',
			'type'        => 'select',
			'label'       => __( 'List', 'sequensy' ),
			'description' => __( 'Select a Campaign Monitor list', 'sequensy' ),
			'required'    => true,
			'dynamic'     => true,
			'endpoint'    => '/integrations/campaignmonitor/connections/{connection_id}/lists',
			'depends_on'  => 'connection_id',
			'value_field' => 'id',
			'label_field' => 'name',
			'order'       => 1,
		),
		array(
			'name'        => 'email',
			'type'        => 'string',
			'label'       => __( 'Email', 'sequensy' ),
			'description' => __( 'Current subscriber email address', 'sequensy' ),
			'required'    => true,
			'order'       => 2,
		),
		array(
			'name'        => 'name',
			'type'        => 'string',
			'label'       => __( 'Name', 'sequensy' ),
			'description' => __( 'Full name of the subscriber', 'sequensy' ),
			'width'       => '1/2',
			'order'       => 3,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'email',
			'type'        => 'string',
			'description' => __( 'Email address of the updated subscriber', 'sequensy' ),
			'example'     => 'subscriber@example.com',
		),
		array(
			'name'        => 'list_id',
			'type'        => 'string',
			'description' => __( 'Campaign Monitor list ID', 'sequensy' ),
			'example'     => 'abc123',
		),
		array(
			'name'        => 'name',
			'type'        => 'string',
			'description' => __( 'Full name of the subscriber', 'sequensy' ),
			'example'     => 'Jane Doe',
		),
		array(
			'name'        => 'new_email',
			'type'        => 'string',
			'description' => __( 'New email address if changed', 'sequensy' ),
			'example'     => 'newemail@example.com',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		$list_id = sanitize_text_field( $config['list_id'] );
		$email   = sanitize_email( $config['email'] );

		if ( ! is_email( $email ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: email address */
					esc_html__( 'Invalid email address: %s', 'sequensy' ),
					esc_attr( $config['email'] )
				)
			);
		}

		$data = array(
			'EmailAddress'   => $email,
			'Resubscribe'    => isset( $config['resubscribe'] ) ? (bool) $config['resubscribe'] : false,
			'ConsentToTrack' => ! empty( $config['consent_to_track'] ) ? sanitize_text_field( $config['consent_to_track'] ) : 'Unchanged',
		);

		if ( ! empty( $config['name'] ) ) {
			$data['Name'] = sanitize_text_field( $config['name'] );
		}

		if ( ! empty( $config['new_email'] ) ) {
			$new_email = sanitize_email( $config['new_email'] );

			if ( ! is_email( $new_email ) ) {
				throw new \RuntimeException(
					sprintf(
						/* translators: %s: email address */
						esc_html__( 'Invalid new email address: %s', 'sequensy' ),
						esc_attr( $config['new_email'] )
					)
				);
			}

			$data['EmailAddress'] = $new_email;
		}

		$result = sequensy_api( 'campaignmonitor' )->update_subscriber( $list_id, $email, $data );

		if ( is_wp_error( $result ) ) {
			throw new \RuntimeException( esc_html( $result->get_error_message() ) );
		}

		return array(
			'email'     => $data['EmailAddress'],
			'list_id'   => $list_id,
			'name'      => ! empty( $config['name'] ) ? sanitize_text_field( $config['name'] ) : '',
			'new_email' => ! empty( $config['new_email'] ) ? sanitize_email( $config['new_email'] ) : '',
		);
	},
);
