<?php
    namespace SEE\WC\CreativeSearch\Presentation\Utils;
    use Impresee\CreativeSearchBar\Domain\Entities\CatalogStatusError;
    use SEE\WC\CreativeSearch\Presentation\Utils\PluginUtils;
    use Impresee\CreativeSearchBar\Domain\UseCases\UpdateImpreseeCatalog;
    use Impresee\CreativeSearchBar\Domain\UseCases\GetImpreseeCatalogState;
    use Impresee\CreativeSearchBar\Domain\UseCases\GetImpreseeConfiguration;
    use SEE\WC\CreativeSearch\Presentation\Models\ImpreseeCatalogStatus2Array;

class SnippetFilename {
    private $utils;
    private $get_impresee_data;
    private $get_snippet_filename;

    public function __construct(
        PluginUtils $utils,
        GetImpreseeConfiguration $get_impresee_config,
        GetImpreseeCatalogState $get_snippet_filename
    ){
        $this->utils = $utils;
        $this->get_impresee_data = $get_impresee_config;
        $this->get_snippet_filename = $get_snippet_filename;
    }   

    private function getImpreseeData(){
        $store = $this->utils->getStore();
        if ($store == NULL){
            return NULL;
        }
        $impresee_data_promise = $this->get_impresee_data->execute($store);
        $impresee_either = $impresee_data_promise->wait();
        $impresee_data = $impresee_either->either(
            function ($failure) { return NULL; },
            function ($impresee_data) { return $impresee_data; }
        );
        return $impresee_data;
    }

    public function getSnippetFilename(String $owner_id){
        $impresee_data = $this->getImpreseeData();
        if ($impresee_data == NULL || $impresee_data->owner_code != $owner_id){
            return ImpreseeCatalogStatus2Array::toArray(new CatalogStatusError);
        }
        $store = $this->utils->getStore();
        $impresee_snippet_filename_promise = $this->get_snippet_filename->execute($impresee_data, $store);
        $impresee_snippet_filename = $impresee_snippet_filename_promise->wait();
        $snippet_filename = $impresee_snippet_filename->either(
            function($failure){ return NULL; },
            function($snippet_filename) { return $snippet_filename; }
        );
        return $snippet_filename;
    }
}