// Licensed to Databricks, Inc. under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  Cloudera, Inc. 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.

export interface ComposeEditorMessage {
  type: 'warning' | 'error';
  line: number;
  pos: number;
  text: string;
}

export interface ComposeEditorElement extends HTMLElement {
  'schema-catalog'?: string;
  'static-url'?: string;
  'initial-value'?: string;
  'catalog-api'?: string;
  'snippets-api'?: string;
  'autocomplete-enabled'?: boolean;
  'autocomplete-web-worker-enabled'?: boolean;
  'auto-format-enabled'?: boolean;
  'read-only'?: boolean;
  dialect: string;
  messages?: ComposeEditorMessage[];
  'on-selection-change'?: (selectionValue: string) => void;
  'on-value-change'?: (newValue: string) => void;
  'on-record-event'?: (record: RecordEvent) => void;
  'on-ace-editor-created'?: (editor: unknown) => void;
}

export interface RecordEvent {
  name: string;
  action: string;
  id?: string;
  attributes?: { [key: string]: unknown };
}

interface ComposeEditorProps {
  dialect: string;
  className: string;
  initialValue?: string;
  autocompleteEnabled?: boolean;
  autocompleteWebWorkerEnabled?: boolean;
  autoFormatEnabled?: boolean;
  readOnly?: boolean;
  schemaCatalog?: Record<string, unknown>;
  catalogApi: Record<string, unknown>;
  snippetsApi: Record<string, unknown>;
  messages?: ComposeEditorMessage[];
  /** Where Web workers are hosted inside a {URL}/webworker/ path */
  staticUrl?: string;
  /**
   * Callback for when the selected statement changes in the query editor
   * @param selectedValue Is the new query text from selection, it is `null` when no selected text available
   */
  onSelectionChange?: (selectedValue: string | null) => void;
  /**
   * Callback for when the text value changes in the query editor
   * @param selectedValue Is the new query text from selection, it is `null` when no selected text available
   */
  onValueChange?: (newValue: string) => void;
  /**
   * Callback for bubbling up tracking events from the component to the main app
   * @param selectedValue Is the new query text from selection, it is `null` when no selected text available
   */
  onRecordEvent?: (record: RecordEvent) => void;
}

declare global {
  namespace JSX {
    // TODO: Find out why the below error appears.
    interface IntrinsicElements {
      'compose-editor': ComposeEditorAttributes;
    }

    interface ComposeEditorAttributes {
      'schema-catalog'?: string;
      'static-url'?: string;
      'initial-value'?: string;
      'catalog-api'?: string;
      'autocomplete-enabled'?: boolean;
      'autocomplete-web-worker-enabled'?: boolean;
      'auto-format-enabled'?: boolean;
      'snippets-api'?: string;
      'read-only'?: boolean;
      dialect?: string;
      messages?: ComposeEditorMessage[];
      // TODO: There seems to be some incompatibility with React and Custom Elements, hence we need to
      // add `ref`, `className`, and `key`. Ideally, these are inherited from React.HTMLAttributes,
      // which doesn't work for some reason.
      ref?: React.RefObject<ComposeEditorElement>;
      class?: string;
      key?: string;
    }
  }
}

declare const isComposeEditorDefined: () => Promise<void>;
