<?php

namespace CouponsPlus\App\Compatibility\Wpml;

use CouponsPlus\App\Compatibility\Wpml\Gettable;
use CouponsPlus\App\Compatibility\Wpml\GettableDecorator;
use CouponsPlus\Original\Collections\Collection;

class OriginalIdGetter 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) && isset($elementTranslations[$sitepress->get_default_language()])) {
                $originalIds->push($elementTranslations[$sitepress->get_default_language()]->element_id);
            }
        }

        return $originalIds->withoutDuplicates();
    } 
}