<?php

namespace WPML\ST\TranslationFile\Sync;

use WPML\Collect\Support\Collection;
use WPML\ST\TranslationFile\StringCollation;

class TranslationUpdates {
	use StringCollation;

	const ICL_STRING_TRANSLATION_COMPLETE = 10;

	private $wpdb;

	private $languageRecords;

	private $data;

	public function __construct( \wpdb $wpdb, \WPML_Language_Records $languageRecords ) {
		$this->wpdb            = $wpdb;
		$this->languageRecords = $languageRecords;
	}

	public function getTimestamp( $domain, $locale ) {
		$this->loadData();
		$lang = $this->languageRecords->get_language_code( $locale );
		return (int) $this->data->get( "$lang#$domain" );
	}

	private function loadData() {
		if ( ! $this->data ) {
			$collation = $this->getCollateForContextColumn( $this->wpdb );
			$sql = "
				SELECT
					CONCAT(st.language,'#',s.context $collation) AS lang_domain,
					UNIX_TIMESTAMP(MAX(st.translation_date)) as last_update
				FROM {$this->wpdb->prefix}icl_string_translations AS st
				INNER JOIN {$this->wpdb->prefix}icl_strings AS s
					ON st.string_id = s.id
				WHERE st.value IS NOT NULL AND st.status = %d
				GROUP BY lang_domain;
			";

			$this->data = wpml_collect(
				$this->wpdb->get_results( $this->wpdb->prepare( $sql, self::ICL_STRING_TRANSLATION_COMPLETE ) )
			)->pluck( 'last_update', 'lang_domain' );
		}
	}

	public function reset() {
		$this->data = null;
	}
}
