import {
    DateFormat, FieldType, ISO8601DateSegment
} from '@terascope/types';
import { toISO8601, trimISODateSegment } from '@terascope/utils';
import {
    FieldTransformConfig,
    ProcessMode,
    FunctionDefinitionType,
    FunctionDefinitionCategory
} from '../interfaces';

export const toYearlyDateConfig: FieldTransformConfig = {
    name: 'toYearlyDate',
    type: FunctionDefinitionType.FIELD_TRANSFORM,
    process_mode: ProcessMode.INDIVIDUAL_VALUES,
    category: FunctionDefinitionCategory.DATE,
    description: 'Converts a value to a yearly ISO 8601 date segment',
    examples: [
        {
            args: { },
            config: {
                version: 1,
                fields: { testField: { type: FieldType.Date, format: DateFormat.iso_8601 } }
            },
            field: 'testField',
            input: '2019-10-22T01:00:00.000Z',
            output: new Date('2019-01-01T00:00:00.000Z').getTime(),
            serialize_output: toISO8601
        },
        {
            args: { },
            config: {
                version: 1,
                fields: { testField: { type: FieldType.Date, format: DateFormat.iso_8601 } }
            },
            field: 'testField',
            input: [new Date('2019-10-22T01:00:00.000Z'), 120],
            output: new Date('2019-01-01T00:00:00.000Z').getTime(),
            serialize_output: toISO8601,
            test_only: true,
        }
    ],
    create() {
        return trimISODateSegment(ISO8601DateSegment.yearly);
    },
    accepts: [
        FieldType.String,
        FieldType.Number,
        FieldType.Date
    ],
    argument_schema: {},
    
(inputConfig) {
        const { field_config } = inputConfig;

        return {
            field_config: {
                description: field_config.description,
                array: field_config.array,
                type: FieldType.Date
            },
        };
    }
};

output_type