<?php

/**
 * Customizations for the online solution.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\General_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\Hook_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Assets\Asset_Location_Enum;
defined('ABSPATH') || exit;
/**
 * Class Online_Solution_Shortcodes
 */
readonly class Online_Solution_Shortcodes
{
    /**
     * Constructor.
     *
     * @param Abstract_Online_Solution_Settings_Sync $syncer The syncer.
     */
    public function __construct(private Abstract_Online_Solution_Settings_Sync $syncer)
    {
        Hook_Helpers::do_scoped_action('northmill_online_shared_online_solution_shortcode_setup', $this->syncer);
        $settings_type = $this->syncer->get_settings_type();
        if (Settings_Type::EXPRESS === $settings_type) {
            // No shortcodes for Express.
            return;
        }
        Abstract_Shared_Base::base_instance()->assets->enquer->register_style('online-solutions/shortcodes', Asset_Location_Enum::FRONTEND);
        add_shortcode('moreflo-content-home-page', array($this, 'content_home_page'));
        add_shortcode('moreflo-content-footer-text', array($this, 'content_footer_text'));
        add_shortcode('moreflo-content-company-info', array($this, 'content_company_info'));
        if (Settings_Type::WEBSHOP !== $settings_type && Settings_Type::CATERING !== $settings_type) {
            // The rest of the shortcodes are only for Webshop and Catering.
            return;
        }
        add_shortcode('moreflo-content-featured-groups', array($this, 'content_company_featured_groups'));
    }
    /**
     * Get the content for the home page shortcode.
     *
     * @return string
     */
    public function content_home_page(): string
    {
        $settings_model = $this->syncer->get_settings_model();
        return wp_kses_post($settings_model->ContentHomePage);
    }
    /**
     * Get the content for the footer text shortcode.
     *
     * @return string
     */
    public function content_footer_text(): string
    {
        $settings_model = $this->syncer->get_settings_model();
        $footer_text = $settings_model->ContentFooterText;
        if (!$footer_text) {
            return '';
        }
        return wp_kses_post(sprintf('<div class="widget-title">%s</div>', __('About us', 'northmill-online-shared')) . wpautop($footer_text));
    }
    /**
     * Get the content for the company info shortcode.
     *
     * @return string
     */
    public function content_company_info(): string
    {
        $settings_model = $this->syncer->get_settings_model();
        $out = array();
        if ($settings_model->CompanyName) {
            $out[] = '<span class="org">' . esc_html($settings_model->CompanyName) . '</span>';
        }
        if ($settings_model->Address1) {
            $out[] = '<span class="street-address">' . esc_html($settings_model->Address1) . '</span>';
        }
        if ($settings_model->Address2) {
            $out[] = '<span class="street-address-2">' . esc_html($settings_model->Address2) . '</span>';
        }
        if ($settings_model->PostCode && $settings_model->City) {
            $out[] = '<span class="postal-code">' . esc_html($settings_model->PostCode) . '</span>&nbsp;<span class="locality">' . esc_html($settings_model->City) . '</span>';
        }
        $out[] = '';
        if ($settings_model->Email) {
            $out[] = 'E-post: <a href="mailto:' . esc_attr($settings_model->Email) . '">' . esc_html($settings_model->Email) . '</a>';
        }
        if ($settings_model->Telephone) {
            $phone = General_Helpers::sanitize_phone_number($settings_model->Telephone);
            $out[] = 'Tel: <a href="tel:' . esc_attr($phone) . '">' . esc_html($settings_model->Telephone) . '</a>';
        }
        return wp_kses_post(wpautop(implode("\n", $out)));
    }
    /**
     * Get the content for the company header shortcode.
     *
     * @param array $attributes Shortcode attributes.
     *
     * @return string
     */
    public function content_company_featured_groups(array $attributes = array()): string
    {
        $settings_model = $this->syncer->get_settings_model();
        $title = $this->syncer->get_settings_type() === Settings_Type::WEBSHOP ? __('Featured groups', 'northmill-online-shared') : '';
        $attributes = shortcode_atts(array('title' => $title, 'ids' => empty($settings_model->FeaturedGroups) ? '' : implode(',', array_map(function ($fg) {
            return $fg->get_wp_id();
        }, $settings_model->FeaturedGroups))), $attributes, 'moreflo-content-featured-groups');
        if (!$attributes['ids']) {
            return '';
        }
        $attributes['ids'] = array_map('trim', explode(',', $attributes['ids']));
        $attributes['terms'] = get_terms(array('taxonomy' => 'product_cat', 'include' => $attributes['ids'], 'meta_key' => 'order', 'orderby' => 'meta_value_num', 'order' => 'ASC'));
        return Abstract_Shared_Base::base_instance()->template_loader->get_template('online-solutions/shortcodes/content-featured-groups', $attributes, \false, \true);
    }
}
