<?php


namespace App\Tables;

use App\Models\Row;
use MightyPork\Utils\Utils;

/**
 * Generates a sequence of string codes for internal naming of columns
 *
 * Produces database-unique lowercase identifiers for created or proposed columns.
 * Column identifiers are used in row objects to uniquely identify data even if the
 * revision header (columns object) is modified, such as reordering, changing name,
 * adding new column in concurrent proposals, etc.
 *
 * Thanks to this uniqueness, it could even be possible to compare or merge forks
 * of the same table.
 */
class ColumnNumerator extends BaseNumerator
{
    const ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

    /**
     * Create numerator for the given number of columns
     *
     * @param int $capacity - how many
     */
    public function __construct($capacity)
    {
        parent::__construct(Row::allocateColIDs($capacity));
    }

    protected function getKey($index)
    {
        return Utils::alphabetEncode($index, self::ALPHABET);
    }
}