<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Admin metabox + AJAX for AnyWareX Clone Translate
 *
 * PluginCheck notes:
 * - Identifiers cannot be placeholders -> must be whitelisted and esc_sql()'d
 * - PluginCheck may still flag safe identifiers coming from helper methods -> use targeted ignore
 */
class AWXCT_Clone_Translate_Admin_Metabox {

	/**
	 * Cache for table columns (request-level).
	 *
	 * @var array<string, string[]>
	 */
	private $cols_cache = array();

	public function __construct() {
		add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) );
		add_action( 'save_post', array( $this, 'save_metabox_data' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );

		add_action( 'wp_ajax_awxct_update_translation_meta', array( $this, 'ajax_update_translation_meta' ) );
		add_action( 'wp_ajax_awxct_remove_translation_entry', array( $this, 'ajax_remove_translation_entry' ) );
	}

	/* ------------------------- helpers ------------------------- */

	private function validate_ident( string $ident ): string {
		$ident = (string) $ident;
		return preg_match( '/^[A-Za-z0-9_]+$/', $ident ) ? $ident : '';
	}

	/**
	 * Whitelisted plugin tables.
	 *
	 * @return array<string,string>
	 */
	private function tables(): array {
		global $wpdb;

		return array(
			'languages'    => $wpdb->prefix . 'awxct_languages',
			'translations' => $wpdb->prefix . 'awxct_translations',
		);
	}

	/**
	 * Return an esc_sql()'d table identifier, but ONLY if it matches our whitelist.
	 *
	 * @param string $key 'languages'|'translations'
	 * @return string Escaped SQL identifier or empty string.
	 */
	private function table_ident_sql( string $key ): string {
		$tables = $this->tables();

		if ( ! isset( $tables[ $key ] ) ) {
			return '';
		}

		$table = (string) $tables[ $key ];

		if ( '' === $this->validate_ident( $table ) ) {
			return '';
		}

		return esc_sql( $table );
	}

	/**
	 * Cached list of columns for a whitelisted table.
	 *
	 * @param string $table Whitelisted table name.
	 * @return string[] Column names (lowercase).
	 */
	private function table_columns( string $table ): array {
		$tables  = $this->tables();
		$allowed = array_values( $tables );

		if ( ! in_array( $table, $allowed, true ) ) {
			return array();
		}

		$table_ident = $this->validate_ident( $table );
		if ( '' === $table_ident ) {
			return array();
		}

		if ( isset( $this->cols_cache[ $table ] ) && is_array( $this->cols_cache[ $table ] ) ) {
			return $this->cols_cache[ $table ];
		}

		$cache_key = 'awxct_cols_' . md5( $table_ident );
		$cols      = wp_cache_get( $cache_key, 'awxct' );

		if ( is_array( $cols ) ) {
			$this->cols_cache[ $table ] = $cols;
			return $cols;
		}

		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
		$cols = (array) $wpdb->get_col( "SHOW COLUMNS FROM `{$table_ident}`", 0 );

		$cols = array_map(
			'strtolower',
			array_filter(
				array_map(
					'strval',
					(array) $cols
				)
			)
		);

		wp_cache_set( $cache_key, $cols, 'awxct', HOUR_IN_SECONDS );
		$this->cols_cache[ $table ] = $cols;

		return $cols;
	}

	private function has_column( string $table, string $col ): bool {
		$col = strtolower( $col );
		return in_array( $col, $this->table_columns( $table ), true );
	}

	private function trans_post_id_col(): string {
		$tables = $this->tables();
		$table  = $tables['translations'];

		$cols = $this->table_columns( $table );

		if ( in_array( 'translated_post_id', $cols, true ) ) {
			return 'translated_post_id';
		}
		if ( in_array( 'translation_post_id', $cols, true ) ) {
			return 'translation_post_id';
		}

		return 'translated_post_id';
	}

	private function trans_table_has_col( string $col ): bool {
		$tables = $this->tables();
		return $this->has_column( $tables['translations'], $col );
	}

	private function languages_order_col( string $table_languages ): string {
		$preferred = array( 'native_name', 'english_name', 'name', 'code' );
		foreach ( $preferred as $col ) {
			if ( $this->has_column( $table_languages, $col ) ) {
				return $col;
			}
		}
		return 'code';
	}

	private function languages_active_col( string $table_languages ): string {
		if ( $this->has_column( $table_languages, 'active' ) ) {
			return 'active';
		}
		if ( $this->has_column( $table_languages, 'is_enabled' ) ) {
			return 'is_enabled';
		}
		return '';
	}

	/**
	 * Determine default language from DB (preferred) and normalize to lowercase with dash.
	 * Falls back to the option 'awxct_default_language'.
	 */
	private function default_lang_norm(): string {
		$blog_id   = function_exists( 'get_current_blog_id' ) ? (int) get_current_blog_id() : 0;
		$cache_key = 'awxct_default_lang_norm_' . $blog_id;
		$cached    = wp_cache_get( $cache_key, 'awxct' );
		if ( is_string( $cached ) && $cached !== '' ) {
			return $cached;
		}

		$default = '';
		global $wpdb;
		$table_languages_sql = $this->table_ident_sql( 'languages' );
		if ( $table_languages_sql ) {
			// Try common default flag columns.
			$flags = array( 'is_default', 'default', 'isDefault' );
			foreach ( $flags as $flag ) {
				$flag_i = $this->validate_ident( $flag );
				if ( '' === $flag_i ) {
					continue;
				}
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
				$code = $wpdb->get_var( "SELECT `code` FROM `{$table_languages_sql}` WHERE COALESCE(`{$flag_i}`,0)=1 LIMIT 1" );
				if ( is_string( $code ) && $code !== '' ) {
					$default = $code;
					break;
				}
			}
		}

		if ( '' === (string) $default ) {
			$default = (string) get_option( 'awxct_default_language', 'en' );
		}

		$norm = strtolower( str_replace( '_', '-', trim( (string) $default ) ) );
		if ( '' === $norm ) {
			$norm = 'en';
		}
		wp_cache_set( $cache_key, $norm, 'awxct', 300 );
		return $norm;
	}

	private function codes_match_tolerant( string $a, string $b ): bool {
		$a = strtolower( str_replace( '_', '-', trim( $a ) ) );
		$b = strtolower( str_replace( '_', '-', trim( $b ) ) );
		if ( '' === $a || '' === $b ) {
			return false;
		}
		if ( $a === $b ) {
			return true;
		}
		$ab = strtok( $a, '-' );
		$bb = strtok( $b, '-' );
		return ( $ab && $bb && $ab === $bb );
	}

	/* ------------------------- UI ------------------------- */

	public function register_metabox() {
		add_meta_box(
			'awxct_metabox',
			__( 'AnyWareX Clone Translate', 'anywarex-clone-translate' ),
			array( $this, 'render_metabox' ),
			array( 'post', 'page' ),
			'side',
			'default'
		);
	}

	public function render_metabox( $post ) {
		global $wpdb;

		$table_languages_sql    = $this->table_ident_sql( 'languages' );
		$table_translations_sql = $this->table_ident_sql( 'translations' );

		if ( '' === $table_languages_sql || '' === $table_translations_sql ) {
			echo '<p>' . esc_html__( 'Configuration error: invalid table identifier.', 'anywarex-clone-translate' ) . '</p>';
			return;
		}

		$post_id_col   = $this->trans_post_id_col();
		$post_id_col_i = $this->validate_ident( $post_id_col );
		if ( '' === $post_id_col_i ) {
			$post_id_col_i = 'translated_post_id';
		}
		$post_id_col_sql = esc_sql( $post_id_col_i );

		// Cache the existence check to satisfy PHPCS 'NoCaching' (same request and object cache).
		$blog_id   = function_exists( 'get_current_blog_id' ) ? (int) get_current_blog_id() : 0;
		$cache_key = 'awxct_entry_exists_' . $blog_id . '_' . absint( $post->ID );
		$entry_exists = wp_cache_get( $cache_key, 'awxct' );
		if ( false === $entry_exists ) {
			// PluginCheck sometimes flags identifiers even if whitelisted+esc_sql()'d when they come from helper methods.
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter
			$entry_exists = $wpdb->get_var(
				$wpdb->prepare(
					// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- identifiers validated/escaped; value via placeholder
					"SELECT id FROM `{$table_translations_sql}` WHERE `{$post_id_col_sql}` = %d LIMIT 1",
					absint( $post->ID )
				)
			);
			wp_cache_set( $cache_key, $entry_exists ? (int) $entry_exists : 0, 'awxct', 300 );
		}
		$entry_exists = (int) $entry_exists;

		if ( ! $entry_exists ) {
			echo '<p>' . esc_html__( 'This page is the original and has no translation attribution (yet).', 'anywarex-clone-translate' ) . '</p>';
			return;
		}

		$selected_lang      = (string) get_post_meta( $post->ID, '_awxct_translate_lang', true );
		$selected_lang_norm = strtolower( str_replace( '_', '-', trim( $selected_lang ) ) );

		$tables          = $this->tables();
		$table_languages = $tables['languages'];

		$active_col = $this->languages_active_col( $table_languages );
		$order_col  = $this->languages_order_col( $table_languages );

		$active_col_i = $active_col ? $this->validate_ident( $active_col ) : '';
		$order_col_i  = $this->validate_ident( $order_col );
		if ( '' === $order_col_i ) {
			$order_col_i = 'code';
		}

		$where_sql = '';
		if ( $active_col_i ) {
			$where_sql = 'WHERE COALESCE(`' . esc_sql( $active_col_i ) . '`,0)=1';
		}

		$order_col_sql = esc_sql( $order_col_i );

		$sql_langs = "SELECT * FROM `{$table_languages_sql}` {$where_sql} ORDER BY `{$order_col_sql}` ASC";

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
		$languages = $wpdb->get_results( $sql_langs );

		$default_lang_norm = $this->default_lang_norm();

		// If someone stored the default language as translation language, treat as "Clone".
		if ( $selected_lang_norm !== '' && $this->codes_match_tolerant( $selected_lang_norm, $default_lang_norm ) ) {
			$selected_lang_norm = '';
		}

		wp_nonce_field( 'awxct_save_metabox', 'awxct_metabox_nonce' );

		echo '<p><label for="awxct_translate_language" style="font-weight:600;">' . esc_html__( 'Language', 'anywarex-clone-translate' ) . '</label></p>';
		echo '<select name="awxct_translate_language" id="awxct_translate_language" class="widefat">';

		// Placeholder: Clone (no language set yet).
		echo '<option value=""' . ( selected( $selected_lang_norm, '', false ) ? ' selected="selected"' : '' ) . '>' . esc_html__( '- Clone -', 'anywarex-clone-translate' ) . '</option>';

		if ( $languages ) {
			foreach ( $languages as $lang ) {
				$code = isset( $lang->code ) ? (string) $lang->code : '';
				$code_norm = strtolower( str_replace( '_', '-', trim( $code ) ) );
				if ( '' === $code_norm ) {
					continue;
				}

				// Hide the default language (tolerant).
				if ( $this->codes_match_tolerant( $code_norm, $default_lang_norm ) ) {
					continue;
				}

				if ( ! empty( $lang->native_name ) ) {
					$label_raw = $lang->native_name;
				} elseif ( ! empty( $lang->english_name ) ) {
					$label_raw = $lang->english_name;
				} elseif ( ! empty( $lang->name ) ) {
					$label_raw = $lang->name;
				} else {
					$label_raw = strtoupper( $code );
				}

				printf(
					'<option value="%1$s"%2$s>%3$s</option>',
					esc_attr( $code_norm ),
					selected( $selected_lang_norm, $code_norm, false ) ? ' selected="selected"' : '',
					esc_html( (string) $label_raw )
				);
			}
		}

		echo '</select>';

		echo '<p style="margin-top:8px;"><button type="button" class="button button-link-delete" id="awxct_remove_translation">';
		echo esc_html__( 'Remove from translations', 'anywarex-clone-translate' );
		echo '</button></p>';

		echo '<input type="hidden" id="awxct_translation_nonce" value="' . esc_attr( wp_create_nonce( 'awxct_translation_nonce' ) ) . '">';
		echo '<input type="hidden" id="awxct_default_lang" value="' . esc_attr( (string) $default_lang_norm ) . '">';
		echo '<input type="hidden" id="awxct_post_id" value="' . esc_attr( (string) $post->ID ) . '">';

		$confirm = __( 'Remove this page from translations? The page itself will NOT be deleted.', 'anywarex-clone-translate' );
		$removed = __( 'Removed from translations.', 'anywarex-clone-translate' );
		$errtxt  = __( 'An error has occurred.', 'anywarex-clone-translate' );
		// JS for this metabox is loaded via enqueued admin script (see enqueue_scripts()).
	}

	/* ------------------------- save ------------------------- */

	public function save_metabox_data( $post_id ) {
		if ( ! isset( $_POST['awxct_metabox_nonce'] ) ) {
			return;
		}

		$nonce = sanitize_text_field( wp_unslash( $_POST['awxct_metabox_nonce'] ) );
		if ( ! wp_verify_nonce( $nonce, 'awxct_save_metabox' ) ) {
			return;
		}

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		$lang_raw = isset( $_POST['awxct_translate_language'] )
			? sanitize_text_field( wp_unslash( $_POST['awxct_translate_language'] ) )
			: '';
		$lang = strtolower( str_replace( '_', '-', trim( (string) $lang_raw ) ) );

		// Do not store the default language as a translation language (treat as Clone).
		$default_lang_norm = $this->default_lang_norm();
		if ( $lang !== '' && $this->codes_match_tolerant( $lang, $default_lang_norm ) ) {
			$lang = '';
		}

		if ( '' === $lang ) {
			delete_post_meta( $post_id, '_awxct_translate_lang' );
		} else {
			update_post_meta( $post_id, '_awxct_translate_lang', $lang );
		}

		global $wpdb;

		$tables      = $this->tables();
		$table       = $tables['translations'];
		$post_id_col = $this->trans_post_id_col();

		$table_i       = $this->validate_ident( $table );
		$post_id_col_i = $this->validate_ident( $post_id_col );
		if ( '' === $table_i || '' === $post_id_col_i ) {
			return;
		}

		$data   = array();
		$format = array();

		if ( $this->trans_table_has_col( 'language_code' ) ) {
			$data['language_code'] = $lang;
			$format[]              = '%s';
		}
		if ( $this->trans_table_has_col( 'is_active' ) ) {
			$data['is_active'] = empty( $lang ) ? 0 : 1;
			$format[]          = '%d';
		}

		if ( $data ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->update(
				$table_i,
				$data,
				array( $post_id_col_i => (int) $post_id ),
				$format,
				array( '%d' )
			);
		}
	}

	/* ------------------------- assets ------------------------- */

	public function enqueue_scripts( $hook ) {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || $screen->base !== 'post' ) {
			return;
		}
		$this->enqueue_assets_common();
	}

	public function enqueue_block_editor_assets() {
		$this->enqueue_assets_common();
	}

	private function enqueue_assets_common() {
		wp_enqueue_script(
			'anywarex-clone-translate-admin',
			function_exists( 'awxct_get_script_url' )
				? awxct_get_script_url( 'assets/js/admin.js' )
				: AWXCT_URL . 'assets/js/admin.js',
			array(),
			defined( 'AWXCT_VERSION' ) ? AWXCT_VERSION : '1.0.0',
			true
		);

		wp_localize_script(
			'anywarex-clone-translate-admin',
			'AWXCTSettings',
			array(
				'ajax'    => admin_url( 'admin-ajax.php' ),
				'ajaxurl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'awxct_translation_nonce' ),
				'i18n'    => array(
					'confirmRemove' => __( 'Remove this page from translations? The page itself will NOT be deleted.', 'anywarex-clone-translate' ),
					'removed'       => __( 'Removed from translations.', 'anywarex-clone-translate' ),
					'error'         => __( 'An error has occurred.', 'anywarex-clone-translate' ),
				),
			)
		);

		wp_add_inline_script(
			'anywarex-clone-translate-admin',
			'window.ajaxurl = window.ajaxurl || (window.AWXCTSettings && (AWXCTSettings.ajax || AWXCTSettings.ajaxurl));',
			'before'
		);

		wp_enqueue_style(
			'anywarex-clone-translate-admin-css',
			function_exists( 'awxct_get_style_url' )
				? awxct_get_style_url( 'assets/css/admin.css' )
				: AWXCT_URL . 'assets/css/admin.css',
			array(),
			defined( 'AWXCT_VERSION' ) ? AWXCT_VERSION : '1.0.0'
		);
	}

	/* ------------------------- ajax ------------------------- */

	public function ajax_update_translation_meta() {
		global $wpdb;

		$tables = $this->tables();
		$table  = $tables['translations'];

		$table_i = $this->validate_ident( $table );
		if ( '' === $table_i ) {
			wp_send_json_error( array( 'message' => 'bad table' ), 500 );
		}

		$table_i = $this->validate_ident( $table );
		if ( '' === $table_i ) {
			wp_send_json_error( array( 'message' => 'bad table' ), 500 );
		}

		$nonce = isset( $_POST['_ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ) : '';
		if ( ! $nonce || ! wp_verify_nonce( $nonce, 'awxct_translation_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'bad nonce' ), 403 );
		}

		$post_id  = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0;
		$language_raw = isset( $_POST['language'] ) ? sanitize_text_field( wp_unslash( $_POST['language'] ) ) : '';
		$language = strtolower( str_replace( '_', '-', trim( (string) $language_raw ) ) );
		$default_lang_norm = $this->default_lang_norm();
		if ( $language !== '' && $this->codes_match_tolerant( $language, $default_lang_norm ) ) {
			$language = '';
		}

		if ( ! $post_id ) {
			wp_send_json_error( array( 'message' => 'no post_id' ), 400 );
		}
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
		}

		$nonce = isset( $_POST['_ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ) : '';
		if ( ! $nonce || ! wp_verify_nonce( $nonce, 'awxct_translation_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'bad nonce' ), 403 );
		}

		if ( '' === $language ) {
			delete_post_meta( $post_id, '_awxct_translate_lang' );
		} else {
			update_post_meta( $post_id, '_awxct_translate_lang', $language );
		}

		$root_post_id = get_post_meta( $post_id, '_awxct_root_post_id', true );
		if ( ! $root_post_id ) {
			$root_post_id = $post_id;
		}

		$post_id_col   = $this->trans_post_id_col();
		$post_id_col_i = $this->validate_ident( $post_id_col );
		if ( '' === $post_id_col_i ) {
			$post_id_col_i = 'translated_post_id';
		}

		if (
			! empty( $language )
			&& $this->trans_table_has_col( 'root_post_id' )
			&& $this->trans_table_has_col( 'language_code' )
			&& $this->trans_table_has_col( 'is_active' )
		) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->update(
				$table_i,
				array( 'is_active' => 0 ),
				array(
					'root_post_id'  => (int) $root_post_id,
					'language_code' => (string) $language,
				),
				array( '%d' ),
				array( '%d', '%s' )
			);
		}

		$data   = array();
		$format = array();

		if ( $this->trans_table_has_col( 'language_code' ) ) {
			$data['language_code'] = $language;
			$format[]              = '%s';
		}
		if ( $this->trans_table_has_col( 'is_active' ) ) {
			$data['is_active'] = empty( $language ) ? 0 : 1;
			$format[]          = '%d';
		}

		if ( $data ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->update(
				$table_i,
				$data,
				array( $post_id_col_i => (int) $post_id ),
				$format,
				array( '%d' )
			);
		}

		wp_send_json_success( array( 'message' => 'updated' ) );
	}

	public function ajax_remove_translation_entry() {
		global $wpdb;

		$tables  = $this->tables();
		$table   = $tables['translations'];
		$table_i = $this->validate_ident( $table );
		if ( '' === $table_i ) {
			wp_send_json_error( array( 'message' => 'bad table' ), 500 );
		}
		
		$nonce = isset( $_POST['_ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ) : '';
		if ( ! $nonce || ! wp_verify_nonce( $nonce, 'awxct_translation_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'bad nonce' ), 403 );
		}

		$post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0;

		if ( ! $post_id ) {
			wp_send_json_error( array( 'message' => 'no post_id' ), 400 );
		}
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
		}



		// Prefer the central mapping class to remove *all* relations for this post
		// (root/original/translated). Fallback to a safe direct query.
		if ( class_exists( 'AWXCT_Clone_Translate_Mapping' ) ) {
			$mapping = new AWXCT_Clone_Translate_Mapping();
			if ( method_exists( $mapping, 'delete_by_post' ) ) {
				$mapping->delete_by_post( $post_id );
			} else {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
				$wpdb->query(
					$wpdb->prepare(
						'DELETE FROM `' . esc_sql( $table_i ) . '` WHERE root_post_id = %d OR original_post_id = %d OR translated_post_id = %d',
						(int) $post_id,
						(int) $post_id,
						(int) $post_id
					)
				);
			}
		} else {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->query(
				$wpdb->prepare(
					'DELETE FROM `' . esc_sql( $table_i ) . '` WHERE root_post_id = %d OR original_post_id = %d OR translated_post_id = %d',
					(int) $post_id,
					(int) $post_id,
					(int) $post_id
				)
			);
		}

		delete_post_meta( $post_id, '_awxct_translate_lang' );

		wp_send_json_success( array( 'message' => 'removed' ) );
	}
}
