<?php

namespace CouponsPlus\App\Compatibility\Wpml;

use CouponsPlus\Original\Collections\Collection;

class TranslationIds implements Gettable
{
    protected $objectType;

    public function __construct(string $objectType) 
    {
        $this->objectType = $objectType;
    }
    
    public function get(int $objectId) : Collection
    {
        global $sitepress;

        (object) $originalIds = new Collection([]);

        (int) $trid = apply_filters( 'wpml_element_trid', NULL, $objectId, $this->objectType);

        if ($trid) {
            (array) $elementTranslations = apply_filters( 'wpml_get_element_translations', NULL, $trid, $this->objectType );

            if (is_array($elementTranslations)) {
                foreach ($elementTranslations as $translationObject) {
                    if (is_object($translationObject)) {
                        $originalIds->push($translationObject->element_id);
                    }
                }
            }
        }

        return $originalIds->withoutDuplicates();
    }
}