<?php

namespace LoftyIDX\includes\pages;
use LoftyIDX\includes\common\LoftyIDXListingHelper;
use LoftyIDX\includes\LoftyIDX;

defined('ABSPATH') || exit;

class LoftyIDXProfilePage extends LoftyIDXPage
{
    protected $metaTags = '';
    public function __construct($template='', $options=[])
    {
        parent::__construct($template, $options);
        $this->getMetaTags();
        $this->changePageTitle();
        add_action('wp_head', [$this, 'injectMetaTags']);
        
    }
    public function getContextData() {
        $mls_list = LoftyIDXListingHelper::get_mls_info();
        $id_list = array_column($mls_list, 'id');
        $data = wp_json_encode([
            'mls_list' => $id_list,
        ]);
        return [
            'moduleId'=> $this->moduleId,
            'mls_list'=> $mls_list,
            'data'=> $data
        ];
    }

    public function getSiteConfig()
    {
        $proxy = LoftyIDX::get_instance()->services['proxy'];
        $site_config = $proxy->get_data('/wp-plugin/admin/profile/init-info');
        if($site_config === false){
            return [];
        }
        return $site_config;
    }
    public function getMetaTags()
    {
        $siteConfig = $this->getSiteConfig();;
        if(isset($siteConfig['data']['pluginPage'])) {
            $tags = $siteConfig['data']['pluginPage']['metaTag'] ?? '';
            $seo_params = $siteConfig['data']['seoParamMap'] ?? [];
            $result = preg_replace_callback('/\{([^}]+)}/', function ($matches) use ($seo_params) {
                return $seo_params[$matches[1]] ?? $matches[0];
            }, $tags);
            $this->metaTags = implode(PHP_EOL, LoftyIDXListingHelper::filterMetaTags($result));
            $this->title = LoftyIDXListingHelper::filterTitle($result);
        }
    }
    public function injectMetaTags()
    {
        echo wp_kses($this->metaTags, $this->allowedTags);
    }
}