import { InvalidationAction } from '@dvn/core/models/actions';
export class FormHandlerCreateDomain {
  constructor(apiDamga, sourceViewId) {
    this.sourceViewId = sourceViewId;
    this.api = apiDamga;
  }
  onSubmit(event) {
    const { values: { domain, overrideHost, productId, target, useTLS } } = event.detail;
    return new Promise((resolve, reject) => {
      const errorHandler = error => {
        if (error.data && error.data.message === 'domain-exists-with-same-address') {
          reject(['Domain already exists.']);
          return;
        }
        reject(['Unexpected error']);
      };
      this.api.createDomain({
        domain,
        productId,
        target: {
          type: 'proxy',
          value: target,
          overrideHost,
          useTLS,
        }
      }).then(() => {
        resolve({
          events: [
            new InvalidationAction(this.sourceViewId, 'table-list-view-domain'),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(errorHandler);
    });
  }
}
