<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\Plugins\UserLanguage;

use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Piwik;
/**
 * @see plugins/UserLanguage/functions.php
 */
require_once PIWIK_INCLUDE_PATH . '/plugins/UserLanguage/functions.php';
/**
 * The UserLanguage API lets you access reports about your Visitors language setting
 *
 * @method static \Piwik\Plugins\UserLanguage\API getInstance()
 */
class API extends \Piwik\Plugin\API
{
    /**
     * @param int|string|int[] $idSite
     * @param string|null|false $segment
     * @return DataTable|DataTable\Map
     */
    protected function getDataTable(string $name, $idSite, string $period, string $date, $segment)
    {
        Piwik::checkUserHasViewAccess($idSite);
        $archive = Archive::build($idSite, $period, $date, $segment);
        $dataTable = $archive->getDataTable($name);
        $dataTable->queueFilter('ReplaceColumnNames');
        $dataTable->queueFilter('ReplaceSummaryRowLabel');
        return $dataTable;
    }
    /**
     * Returns visitor language metrics grouped by base language code.
     *
     * @param int|string|int[] $idSite Website ID(s) to query.
     *                                 - Single site ID (e.g. 1)
     *                                 - Multiple site IDs (e.g. [1, 4, 5])
     *                                 - Comma-separated list ("1,4,5") or "all"
     * @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
     *                                                   containing the specified date.
     * @param string $date The date or date range to process.
     *                     'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth, lastYear),
     *                     or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX, previousX).
     * @param string|null|false $segment Custom segment to filter the report.
     *                                   Example: "referrerName==example.com"
     *                                   Supports AND (;) and OR (,) operators.
     * @return DataTable|DataTable\Map Visitor language metrics grouped by language code.
     */
    public function getLanguage($idSite, string $period, string $date, $segment = \false)
    {
        $dataTable = $this->getDataTable(\Piwik\Plugins\UserLanguage\Archiver::LANGUAGE_RECORD_NAME, $idSite, $period, $date, $segment);
        $dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\\groupByLangCallback'));
        $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'segment', function ($label) {
            if (empty($label) || $label == 'xx') {
                return 'languageCode==xx';
            }
            return sprintf('languageCode==%1$s,languageCode=@%1$s-', $label);
        }));
        $dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\\languageTranslate'));
        return $dataTable;
    }
    /**
     * Returns visitor language metrics grouped by full locale code.
     *
     * @param int|string|int[] $idSite Website ID(s) to query.
     *                                 - Single site ID (e.g. 1)
     *                                 - Multiple site IDs (e.g. [1, 4, 5])
     *                                 - Comma-separated list ("1,4,5") or "all"
     * @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data for the period
     *                                                   containing the specified date.
     * @param string $date The date or date range to process.
     *                     'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth, lastYear),
     *                     or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX, previousX).
     * @param string|null|false $segment Custom segment to filter the report.
     *                                   Example: "referrerName==example.com"
     *                                   Supports AND (;) and OR (,) operators.
     * @return DataTable|DataTable\Map Visitor language metrics grouped by locale code.
     */
    public function getLanguageCode($idSite, string $period, string $date, $segment = \false)
    {
        $dataTable = $this->getDataTable(\Piwik\Plugins\UserLanguage\Archiver::LANGUAGE_RECORD_NAME, $idSite, $period, $date, $segment);
        $dataTable->filter('AddSegmentValue');
        $dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\\languageTranslateWithCode'));
        return $dataTable;
    }
}
