/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {Type} from '../../interface/type';
import {fillProperties} from '../../util/property';
import {EMPTY_ARRAY, EMPTY_OBJ} from '../empty';
import {ComponentDef, DirectiveDef, DirectiveDefFeature, RenderFlags} from '../interfaces/definition';
import {isComponentDef} from '../util/view_utils';

import {NgOnChangesFeature} from './ng_onchanges_feature';

function getSuperType(type: Type<any>): Type<any>&
    {ngComponentDef?: ComponentDef<any>, ngDirectiveDef?: DirectiveDef<any>} {
  return Object.getPrototypeOf(type.prototype).constructor;
}

/**
 * Merges the definition from a super class to a sub class.
 * @param definition The definition that is a SubClass of another directive of component
 */
export function InheritDefinitionFeature(definition: DirectiveDef<any>| ComponentDef<any>): void {
  let superType = getSuperType(definition.type);

  while (superType) {
    let superDef: DirectiveDef<any>|ComponentDef<any>|undefined = undefined;
    if (isComponentDef(definition)) {
      // Don't use getComponentDef/getDirectiveDef. This logic relies on inheritance.
      superDef = superType.ngComponentDef || superType.ngDirectiveDef;
    } else {
      if (superType.ngComponentDef) {
        throw new Error('Directives cannot inherit Components');
      }
      // Don't use getComponentDef/getDirectiveDef. This logic relies on inheritance.
      superDef = superType.ngDirectiveDef;
    }

    const baseDef = (superType as any).ngBaseDef;

    // Some fields in the definition may be empty, if there were no values to put in them that
    // would've justified object creation. Unwrap them if necessary.
    if (baseDef || superDef) {
      const writeableDef = definition as any;
      writeableDef.inputs = maybeUnwrapEmpty(definition.inputs);
      writeableDef.declaredInputs = maybeUnwrapEmpty(definition.declaredInputs);
      writeableDef.outputs = maybeUnwrapEmpty(definition.outputs);
    }

    if (baseDef) {
      // Merge inputs and outputs
      fillProperties(definition.inputs, baseDef.inputs);
      fillProperties(definition.declaredInputs, baseDef.declaredInputs);
      fillProperties(definition.outputs, baseDef.outputs);
    }

    if (superDef) {
      // Merge hostBindings
      const prevHostBindings = definition.hostBindings;
      const superHostBindings = superDef.hostBindings;
      if (superHostBindings) {
        if (prevHostBindings) {
          definition.hostBindings = (rf: RenderFlags, ctx: any, elementIndex: number) => {
            superHostBindings(rf, ctx, elementIndex);
            prevHostBindings(rf, ctx, elementIndex);
          };
        } else {
          definition.hostBindings = superHostBindings;
        }
      }

      // Merge View Queries
      if (isComponentDef(definition) && isComponentDef(superDef)) {
        const prevViewQuery = definition.viewQuery;
        const superViewQuery = superDef.viewQuery;
        if (superViewQuery) {
          if (prevViewQuery) {
            definition.viewQuery = <T>(rf: RenderFlags, ctx: T): void => {
              superViewQuery(rf, ctx);
              prevViewQuery(rf, ctx);
            };
          } else {
            definition.viewQuery = superViewQuery;
          }
        }
      }

      // Merge Content Queries
      const prevContentQueries = definition.contentQueries;
      const superContentQueries = superDef.contentQueries;
      if (superContentQueries) {
        if (prevContentQueries) {
          definition.contentQueries = <T>(rf: RenderFlags, ctx: T, directiveIndex: number) => {
            superContentQueries(rf, ctx, directiveIndex);
            prevContentQueries(rf, ctx, directiveIndex);
          };
        } else {
          definition.contentQueries = superContentQueries;
        }
      }

      // Merge inputs and outputs
      fillProperties(definition.inputs, superDef.inputs);
      fillProperties(definition.declaredInputs, superDef.declaredInputs);
      fillProperties(definition.outputs, superDef.outputs);

      // Inherit hooks
      // Assume super class inheritance feature has already run.
      definition.afterContentChecked =
          definition.afterContentChecked || superDef.afterContentChecked;
      definition.afterContentInit = definition.afterContentInit || superDef.afterContentInit;
      definition.afterViewChecked = definition.afterViewChecked || superDef.afterViewChecked;
      definition.afterViewInit = definition.afterViewInit || superDef.afterViewInit;
      definition.doCheck = definition.doCheck || superDef.doCheck;
      definition.onDestroy = definition.onDestroy || superDef.onDestroy;
      definition.onInit = definition.onInit || superDef.onInit;

      // Run parent features
      const features = superDef.features;
      if (features) {
        for (const feature of features) {
          if (feature && feature.ngInherit) {
            (feature as DirectiveDefFeature)(definition);
          }
        }
      }
    } else {
      // Even if we don't have a definition, check the type for the hooks and use those if need be
      const superPrototype = superType.prototype;
      if (superPrototype) {
        definition.afterContentChecked =
            definition.afterContentChecked || superPrototype.ngAfterContentChecked;
        definition.afterContentInit =
            definition.afterContentInit || superPrototype.ngAfterContentInit;
        definition.afterViewChecked =
            definition.afterViewChecked || superPrototype.ngAfterViewChecked;
        definition.afterViewInit = definition.afterViewInit || superPrototype.ngAfterViewInit;
        definition.doCheck = definition.doCheck || superPrototype.ngDoCheck;
        definition.onDestroy = definition.onDestroy || superPrototype.ngOnDestroy;
        definition.onInit = definition.onInit || superPrototype.ngOnInit;

        if (superPrototype.ngOnChanges) {
          NgOnChangesFeature()(definition);
        }
      }
    }

    superType = Object.getPrototypeOf(superType);
  }
}

function maybeUnwrapEmpty<T>(value: T[]): T[];
function maybeUnwrapEmpty<T>(value: T): T;
function maybeUnwrapEmpty(value: any): any {
  if (value === EMPTY_OBJ) {
    return {};
  } else if (value === EMPTY_ARRAY) {
    return [];
  } else {
    return value;
  }
}
