<?php

namespace App\Browser\Jobs\InstagramRepost\DescriptionPipeline;

/**
 * Step to remove account references from the description.
 */

class RemoveAccountsReferenceStep implements IInstagramDescriptionPipelineStep
{
    /**
     * Process the description to remove account references.
     *
     * @param string $description
     * @return string
     */
    public function process(string $description): string
    {
        // Regular expression to match Instagram account references
        $pattern = '/@([a-zA-Z0-9._]+)/';

        // Split by lines
        $lines = explode("\n", $description);

        // Remove lines that contain account references
        $lines = array_filter($lines, function ($line) use ($pattern) {
            // Check if the line does not match the account reference pattern
            return !preg_match($pattern, $line);
        });

        // Join the remaining lines back into a single string
        $cleanedDescription = implode("\n", $lines);

        // Trim whitespace from the beginning and end of the description
        return trim($cleanedDescription);
    }
}
