<?php
declare(strict_types=1);

namespace Autogedal\FrontendI18n\Model;

class AreaPathsResolver
{
    public const AREA_FRONTEND = 'frontend';
    public const AREA_BACKEND = 'backend';

    /**
     * @return string[]
     */
    public function resolve(string $area, bool $includeVendor = false): array
    {
        $this->assertValidArea($area);

        $area = strtolower(trim($area));

        if ($area === self::AREA_FRONTEND) {
            $paths = [
                BP . '/app/design/frontend',
                BP . '/app/code/*/*/view/frontend',
                BP . '/app/code/*/*/Block',
                BP . '/app/code/*/*/Controller',
            ];

            if ($includeVendor) {
                $paths = array_merge($paths, [
                    BP . '/vendor/*/*/view/frontend',
                    BP . '/vendor/*/*/Block',
                    BP . '/vendor/*/*/Controller',
                ]);
            }

            return $paths;
        }

        $paths = [
            BP . '/app/design/adminhtml',
            BP . '/app/code/*/*/view/adminhtml',
            BP . '/app/code/*/*/Block/Adminhtml',
            BP . '/app/code/*/*/Controller/Adminhtml',
        ];

        if ($includeVendor) {
            $paths = array_merge($paths, [
                BP . '/vendor/*/*/view/adminhtml',
                BP . '/vendor/*/*/Block/Adminhtml',
                BP . '/vendor/*/*/Controller/Adminhtml',
            ]);
        }

        return $paths;
    }

    public function assertValidArea(string $area): void
    {
        if (!in_array(strtolower(trim($area)), [self::AREA_FRONTEND, self::AREA_BACKEND], true)) {
            throw new \InvalidArgumentException('The --area option must be "frontend" or "backend".');
        }
    }

}
