<?php

namespace Breakdance\DynamicData;

class ArchiveTitle extends StringField
{

    /**
     * @inheritDoc
     */
    public function label()
    {
        return __('Archive Title', 'breakdance');
    }

    /**
     * @inheritDoc
     */
    public function category()
    {
        return __('Archive', 'breakdance');
    }

    /**
     * @inheritDoc
     */
    public function slug()
    {
        return 'archive_title';
    }


    /**
     * @inheritDoc
     */
    public function controls()
    {
        return [
            \Breakdance\Elements\control('disable_prefix', __('Disable Prefix', 'breakdance'), [
                'type' => 'toggle'
            ]),
        ];
    }


    /**
     * @inheritDoc
     */
    public function handler($attributes): StringData
    {
        $disablePrefix = filter_var($attributes['disable_prefix'] ?? false, FILTER_VALIDATE_BOOLEAN);
        if ($disablePrefix) {
            add_filter('get_the_archive_title_prefix', '__return_false');
        }

        $title = StringData::fromString(get_the_archive_title());

        if ($disablePrefix) {
            remove_filter('get_the_archive_title_prefix', '__return_false');
        }

        return $title;
    }
}
