/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import React, { Fragment } from 'react';

import {
  EuiFieldNumber,
  EuiFormRow,
} from '@elastic/eui';

import {
  DefaultFormatEditor
} from '../default';

import {
  FormatEditorSamples
} from '../../samples';

import {
  sample
} from './sample';

import { FormattedMessage } from '@kbn/i18n/react';

export class TruncateFormatEditor extends DefaultFormatEditor {
  static formatId = 'truncate';

  constructor(props) {
    super(props);
    this.state.sampleInputs = [sample];
  }

  render() {
    const { formatParams } = this.props;
    const { error, samples } = this.state;

    return (
      <Fragment>
        <EuiFormRow
          label={<FormattedMessage id="common.ui.fieldEditor.truncate.lengthLabel" defaultMessage="Field length"/>}
          isInvalid={!!error}
          error={error}
        >
          <EuiFieldNumber
            defaultValue={formatParams.fieldLength}
            onChange={(e) => {
              this.onChange({ fieldLength: e.target.value ? Number(e.target.value) : null });
            }}
            isInvalid={!!error}
          />
        </EuiFormRow>
        <FormatEditorSamples
          samples={samples}
        />
      </Fragment>
    );
  }
}
