declare namespace datasources {
  type TEventData =
    import('~shared/hooks/use-events/interfaces').webforms.WEvent;
  type TDatasourceType =
    | 'auto'
    | 'entity'
    | 'entitysel'
    | 'catalog'
    | 'dataclass'
    | 'scalar'
    | 'array'
    | 'object';
  type TJSType = 'string' | 'number' | 'bool' | 'date' | 'object' | 'array';
  type TDataType =
    | 'array'
    | 'bool'
    | 'string'
    | 'text'
    | 'uuid'
    | 'short'
    | 'long'
    | 'number'
    | 'long64'
    | 'duration'
    | 'object'
    | 'date'
    | 'image';
  interface IEnhancedCatalog extends ICatalog {
    dataClasses: IDataClasses;
    dataClassByCollectionName: IDataClasses;
    singletons: ISingletons;
  }
  interface ICollection<T extends IEntity = IEntity> {
    __COUNT: number;
    __DATACLASS: string;
    __FIRST: number;
    __GlobalStamp: number;
    __SENT: string;
    __entityModel: string;
    __ENTITIES: T[];
    __ENTITYSET?: string;
    __MAXENTITIES?: number;
    __queryPlan?: any;
    __extendedQueryPlan?: any;
    __extendedQueryPath?: any;
  }

  interface IBaseAttribute {
    name: string;
    kind: AttributeKind;
    scope: Scope;
    exposed?: boolean;
    readOnly?: boolean;
  }
  interface ISimpleAttribute extends IBaseAttribute {
    type: AttributeType;
    indexed?: boolean;
    identifying?: boolean;
    simpleDate?: boolean;
  }

  interface IRelatedEntitiesAttribute extends IBaseAttribute {
    kind: 'relatedEntities';
    path: string;
    type: AttributeType | string;
    inverseName: string;
    reversePath: boolean;
  }

  interface IRelatedEntityAttribute extends IBaseAttribute {
    kind: 'relatedEntity';
    type: AttributeType | string;
    path: string;
    foreignKey: string;
    inverseName: string;
  }

  interface ICalculatedAttribute extends IBaseAttribute {
    kind: 'calculated';
    type: AttributeType | string;
    foreignKey?: string;
    inverseName?: string;
    reversePath?: boolean;
    path?: string;
    behavior?: 'relatedEntities' | 'relatedEntity';
  }

  interface IAliasAttribute extends IBaseAttribute {
    behavior?: 'relatedEntities' | 'relatedEntity';
    type: AttributeType | string;
    foreignKey?: string;
    inverseName?: string;
    reversePath?: boolean;
    path?: string;
    kind: 'alias';
  }

  type IAttribute = {
    name: string;
    type: string;
    kind: EAttributeKind;
    scope: AttributeScope;
    uuid?: string;
  } & Partial<{
    indexKind: TIndexType;
    autogenerate: boolean;
    unique: boolean;
    primKey: boolean;
    exposed: boolean;
    behavior: string;
    autosequence: boolean;
    reversePath: boolean;
    not_null: boolean;
    minValue: number;
    maxValue: number;
    simpleDate: boolean;
    defaultValue: any;
    events: any[];
    path: string;
    extraProperties: IAttributeProperties;
    styled_text: boolean;
  }>;

  interface IRelatedAttribute extends IBaseAttribute {
    type?: AttributeType | string;
    behavior: 'relatedEntities' | 'relatedEntity';
    foreignKey?: string;
    inverseName?: string;
    path?: string;
    reversePath?: boolean;
  }
  interface IEntity {
    __KEY?: string;
    __TIMESTAMP?: Date;
    __STAMP?: number;
  }

  type TSingletonMethod = Partial<catalog.IMethod>;

  interface ISingletonClass {
    name: string;
    methods: ISingletonMethod[];
  }
  interface ICatalog {
    __UNIQID: string;
    __BASEID: string;
    methods: catalog.IMethod[];
    dataClasses: IDetailedDataClass[];
    singletons?: ISingletonClass[];
  }

  interface IDataClass {
    name: string;
    uri: string;
    dataURI: string;
  }
  interface IDetailedDataClass extends IDataClass {
    className: string;
    collectionName: string;
    tableNumber: number;
    scope: Scope;
    attributes?: IAttribute[];
    key: IDataClassKey[];
    methods: catalog.IMethod[];
    exposed?: boolean;
    extraProperties?: IAttributeProperties;
    virtual?: boolean;
    uuid?: string;
  }

  type IDataClasses = { [key: string]: IDetailedDataClass };

  type ISingletons = { [key: string]: ISingletonClass };

  class AttributeFilter {
    addAttribute(attname: string, subfilter?: any): void;
    mergeWith(mergeWith: { filter: any }): void;
    addFilterText(filterText: string, newprop: string): string;
    buildString(curpath: string): string;
  }

  interface IDSChildren {
    [datasourceName: string]: DataSource;
  }

  interface INameSpace {
    sources: IDSChildren;
    root: DataSource;
  }

  type ICreateDataSource =
    | ICreateDataSourceScalar
    | ICreateDataSourceArray
    | ICreateDataSourceObject
    | ICreateDataSourceEntity
    | ICreateDataSourceEntitySelection;
  type IEntityDatasource =
    | datasources.ICreateDataSourceEntity
    | datasources.ICreateDataSourceEntitySelection;

  interface ICreateDataSourceCommon {
    id: string;
    initialValue: any;
    events?: webforms.WEvent[];
    namespace: string;
    schema?: {
      type: string;
      properties: { name: string; type: string; initialValue: any }[];
    };
  }

  interface ICreateDataSourceScalar extends ICreateDataSourceCommon {
    type: 'scalar';
    initialValue?: string | boolean | number | any[] | object | null;
    dataType: TJSType;
    from?: string;
  }

  interface ICreateDataSourceArray extends ICreateDataSourceCommon {
    id: string;
    type: 'scalar';
    initialValue: any[] | null;
    dataType: 'array';
    from?: string;
  }
  interface ICreateDataSourceObject extends ICreateDataSourceCommon {
    type: 'scalar';
    initialValue: object | null;
    dataType: 'object';
    from?: string;
  }
  interface ICreateDataSourceEntity extends ICreateDataSourceCommon {
    type: 'entity';
    initialValue: 'first' | 'empty' | '';
    dataclass: string;
    depth: number | null;
    from?: string;
    dataType?: string;
  }
  interface ICreateDataSourceEntitySelection extends ICreateDataSourceCommon {
    type: 'entitysel';
    initialValue: 'all' | '';
    from?: string;
    dataclass: string;
    depth: number | null;
    pageSize: number | null;
    dataType?: string;
  }

  interface IDSMethods {
    [key: string]: catalog.IMethod;
  }

  class DataSource {
    id: string;
    alias: string;
    type: TDatasourceType;
    namespace: string;
    stamp: number;
    frozenStamp: number;
    inited: boolean;
    beeingResolved: boolean;
    fromServerStorage: boolean;
    canBeUpdatedByParent: boolean;
    local: boolean;
    parentSource: this;
    dataType: TJSType;
    dataclass: datasources.DataClass;
    dataclassID: string;
    parendId: string;
    children: IDSChildren;
    initialValue: string | null;
    properyInParent?: string;
    filterAttributes?: AttributeFilter;
    filterAttributesText?: string;

    constructor(namespace: string, id: string, type: string);
    static spaces: {
      [namespace: string]: INameSpace;
    };
    static globalStamp: number;
    static anonymCount: number;
    static dataTypeMap: {
      bool: 'bool';
      string: 'string';
      text: 'string';
      uuid: 'string';
      short: 'number';
      long: 'number';
      number: 'number';
      long64: 'number';
      duration: 'number';
      object: 'object';
      date: 'date';
    };
    static removeSource(id: string, namespace?: string): void;
    static getOrBuildNameSpace(namespace?: string): INameSpace;
    static getNameSpace(namespace: string): INameSpace;
    static buildSource(
      sourcestr: string,
      initialValue: any,
      ds: DataStore,
      namespace: string,
    ): void;
    static create(
      settings: ICreateDataSource,
      ds: DataStore,
      namespace?: string,
    ): DataSource;
    static getSourceAttributes(
      id: string,
      options?: any,
      namespace?: string,
    ): IDataClassAttribute[];
    static buildCatalogDatasources(
      datastore: datasources.DataStore,
      namespace?: string,
    );
    isLocal(): boolean;
    setLocal(local: boolean): void;
    buildCallFuncURL(
      methodName: string,
      added: string,
    ): string | Promise<string>;
    buildEntitySettings(existingSettings?: any): any;
    buildSelectionSettings(existingSettings?: any): any;
    fireEvent(eventType: string, data?: any): void;
    addListener(eventType: string, callback: Function): void;
    removeListener(eventType: string, callback: Function): void;
    getValue<T = any>(property?: string | number, settings?: any): Promise<T>;
    findElementPosition(elementDataSource: DataSource): Promise<number>;
    getCollection<T extends IEntity = IEntity>(
      index: number,
      howmany: number,
      propertyStr: string,
    ): Promise<T[]>;
    orderBy(attrs: string, settings?: any): Promise<null>;
    query(queryStr: string, settings?: any): Promise<null>;
    getValueAsParam(property?: string, settings?: any): Promise<null>;
    getDataType(): null;
    setValue<T = any>(
      property: string | null,
      value: T,
      doFireEvent?: boolean,
    ): Promise<void>;
    setValueFromMethodResult(resp: any, doFireEvent?: boolean): Promise<void>;
    clone(): null;
    refresh(doFireEvent?: boolean): Promise<void>;
    recompute(fireEvent?: boolean): Promise<void>;
    recomputeChildren(doFireEvent?: boolean): Promise<void>;
    addAttributeFilter(child: this): void;
    static createRequestOptimization(ns?: string): void;
    formatValue(value: any): typeof value;
    getStamp(): number;
    getPageSize(): number;
    touch(): void;
    getParentSource(): this;
    getDataClass(): DataClass;
    getProperyInParent(): string;
    freezeStamp(withChildren: boolean): void;
    modifiedSinceLastFreeze(): boolean;
    dumpTree(curtext: string, level: number): string;
    init(): void;
    filterAttributesText?: string;
    callAction?(action: string, meta?: any): void;
    getMethods(): IDSMethods;
    copyValueInto(source: any, fireEvent?: boolean): Promise<void>;
    static getSource(id: string, namespace?: string): DataSource | undefined;
    static getSource(id: string, namespace?: string): DataSource | undefined;
    static getSourceOrBuild(
      id: string | null,
      initialValue: any,
      ds: DataStore,
      namespace?: string,
    ): DataSource;
    static AddAlias(id: string, datasource: DataSource, namespace?: string);
    static buildDependencies(namespace?: string): void;
    static computeInitialValues(namespace?: string): void;
    static refreshNamespace(namespace?: string): void;
    static getAllSources(namespace?: string): IDSChildren | {};
  }
}
