<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_AdvanceSeoManager
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\AdvanceSeoManager\Model\Config\Source;

class PagesList implements \Magento\Framework\Option\ArrayInterface
{

    public function __construct(
        \Magento\Cms\Api\PageRepositoryInterface $pageRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->_cmsPage = $pageRepository;
        $this->_search = $searchCriteriaBuilder;
    }

    public function toOptionArray()
    {
        $pages = [];
        $pages = [
            0 => ['value' => 'catalog_category_view',
                'label' => 'Category Pages'],
            1 => ['value' => 'catalog_product_view',
                'label' => 'Product Pages'],
            2 => ['value' => 'checkout_index_index',
            'label' => 'Checkout'],
            3 => ['value' => 'checkout_cart_index',
            'label' => 'Cart']
        ];
        foreach ($this->_cmsPage->getList($this->_getSearchCriteria())->getItems() as $page) {
            $pages[] = [
                'value' => '/'.$page->getIdentifier().'/',
                'label' => 'CMS Page: '.$page->getTitle(),
            ];
        }
        return $pages;
    }

    protected function _getSearchCriteria()
    {
        return $this->_search->addFilter('is_active', '1')->create();
    }
}
