import { T as TextField } from './index-ec7dc2ad.js';
import { D as DataModel } from './data-model-bb5789f5.js';
import { V as ViewPlacement } from './app-config-cf28072f.js';
import './FieldModelGenerator-6ec60042.js';
import { D as DateField, a as DateDataType } from './date-b270be76.js';
import { S as SingleChoiceField, a as SingleChoiceFieldOption } from './index-94c1d7bf.js';
import { S as Space } from './table-config-c0c15e50.js';
import { D as DataTransferField } from './data-transfer-field-1ad95e08.js';
import { C as Comparison } from './filter-cbaa3d7d.js';
import { B as BooleanField } from './index-44e741ab.js';
import { F as FormViewConfig, M as ModifierTrimmer } from './form-view-config-bb777190.js';
import { A as AppViewType } from './view-config-3ab16b28.js';
import { I as InvalidationAction } from './actions-b9a0f808.js';
import { p as prod } from './services-fc4ce8f3.js';
import { d as dataConfigs$1, V as ValidatorRegex, D as DvnDataAdapter, T as TableListViewConfig } from './table-list-view-config-80e66dd9.js';
import { N as NestedField } from './nested-15d95467.js';

const damgaSpace = Space.decode({
  _id: 'asdqweasd',
  _created: new Date(),
  _updated: new Date(),
  name: 'Asdqweasd',
});
const dataConfigs = {
  'damga-domains': DataModel.decode({
    _id: 'damga-domains',
    base: prod.base,
    fields: [
      new DateField('_created', 'Created')
        .setDataType(DateDataType.Unix)
        .setDateFormat('DD MMM YYYY HH:mm'),
      new TextField('domain', 'Domain').setInputType('url'),
    ],
    name: 'damga-domains',
    space: damgaSpace,
  }),
};

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);
    });
  }
}

const ID = 'new-domain-and-target-form';
const domainRegex = '(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]';
const config = (apiDamga) => FormViewConfig.decode({
  _id: ID,
  base: 'devingen-dev',
  title: 'New domain',
  space: damgaSpace,
  table: dataConfigs$1['damga-domains'],
  type: AppViewType.FormView,
  handler: new FormHandlerCreateDomain(apiDamga, ID),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('productId'),
      },
      {
        field: new TextField('domain', 'Domain', 'acme.co', 'SSL certificate will be generated for this domain.').addInterceptor('onBlur', new ValidatorRegex(domainRegex, 'Not a valid domain.'))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new TextField('target', 'Target', 'acme.co or 1.2.3.4:5678', 'Traffic will be forwarded to this address.').addInterceptor('onBlur', new ValidatorRegex(domainRegex, 'Not a valid domain or address.'))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new BooleanField('overrideHost', 'Override Host', 'Overrides the target host with domain while accessing the target.'),
      },
      {
        field: new BooleanField('useTLS', 'Use TLS', 'Uses **https** instead of **http** while accessing the target.'),
      }
    ],
  },
  params: {
    overrideHost: {
      required: true,
      value: true,
    },
    useTLS: {
      required: true,
      value: false,
    },
  },
});

class FormHandlerUpdateDomain {
  constructor(apiDamga, sourceViewId) {
    this.sourceViewId = sourceViewId;
    this.api = apiDamga;
  }
  onSubmit(event) {
    const { values: { domainId, overrideHost, target, useTLS } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.updateDomain(domainId, {
        target: {
          type: 'proxy',
          value: target,
          overrideHost,
          useTLS,
        }
      }).then(() => {
        resolve({
          events: [
            new InvalidationAction(this.sourceViewId, 'table-list-view-domain'),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

const ID$1 = 'update-domain-and-target-form';
const domainRegex$1 = '(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]';
const config$1 = (apiDamga) => FormViewConfig.decode({
  _id: ID$1,
  base: 'devingen-dev',
  title: 'Update domain',
  space: damgaSpace,
  table: dataConfigs$1['damga-domains'],
  type: AppViewType.FormView,
  handler: new FormHandlerUpdateDomain(apiDamga, ID$1),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('domainId'),
      },
      {
        field: new TextField('domain', 'Domain').disable()
          .require(),
      },
      {
        field: new TextField('target', 'Target', 'acme.co or 1.2.3.4:5678', 'Traffic will be forwarded to this address.').addInterceptor('onBlur', new ValidatorRegex(domainRegex$1, 'Not a valid domain or address.'))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new BooleanField('overrideHost', 'Override Host', 'Overrides the target host with domain while accessing the target.'),
      },
      {
        field: new BooleanField('useTLS', 'Use TLS', 'Uses **https** instead of **http** while accessing the target.'),
      }
    ],
  },
  params: {
    domainId: {
      required: true,
    },
    domain: {
      required: true,
    },
    target: {
      required: true,
    },
    overrideHost: {
      required: true,
    },
    useTLS: {
      required: true,
    },
  },
});

class FormHandlerMoveDomain {
  constructor(apiDamga, sourceViewId) {
    this.sourceViewId = sourceViewId;
    this.api = apiDamga;
  }
  onSubmit(event) {
    const { values: { domainId, targetProductId } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.moveDomain(domainId, {
        productId: targetProductId,
      }).then(() => {
        resolve({
          events: [
            new InvalidationAction(this.sourceViewId, 'table-list-view-domain'),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

const ID$2 = 'form-view-domain-move';
const config$2 = (apiDamga, products, workspaceOwnerships) => FormViewConfig.decode({
  _id: ID$2,
  base: 'devingen-dev',
  title: 'Move domain',
  space: damgaSpace,
  table: dataConfigs$1['damga-domains'],
  type: AppViewType.FormView,
  handler: new FormHandlerMoveDomain(apiDamga, ID$2),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('domainId'),
      },
      {
        field: new SingleChoiceField('targetProductId', 'Select product to move this domain', products
          .filter(p => p.type === 'damga')
          .map(product => (new SingleChoiceFieldOption(product.name, product._id, 'primary', workspaceOwnerships.find(w => w.workspace._id === product.workspace._id).workspace.name))), '', 'radioCard').setDataAdapter(new DvnDataAdapter('name', '_id'))
          .require(),
      },
    ],
    i18n: {
      submit: 'Move Domain'
    }
  },
  params: {
    domainId: {
      required: true,
    },
    domain: {
      required: true,
    },
    target: {
      required: true,
    },
    overrideHost: {
      required: true,
    },
    useTLS: {
      required: true,
    },
  },
});

class FormHandlerDeleteDomain {
  constructor(apiDamga, sourceViewId) {
    this.sourceViewId = sourceViewId;
    this.api = apiDamga;
  }
  onSubmit(event) {
    const { values: { domainId } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.deleteDomain(domainId).then(() => {
        resolve({
          events: [
            new InvalidationAction(this.sourceViewId, 'table-list-view-domain'),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

const ID$3 = 'form-view-domain-delete';
const config$3 = (apiDamga) => FormViewConfig.decode({
  _id: ID$3,
  base: 'devingen-dev',
  title: 'Are you sure to delete the domain?',
  space: damgaSpace,
  table: dataConfigs$1['damga-domains'],
  type: AppViewType.FormView,
  handler: new FormHandlerDeleteDomain(apiDamga, ID$3),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('domainId'),
      },
    ],
    i18n: {
      submit: 'Delete Domain',
    }
  },
  params: {
    domainId: {
      required: true,
    },
    domain: {
      required: true,
    },
    target: {
      required: true,
    },
    overrideHost: {
      required: true,
    },
    useTLS: {
      required: true,
    },
  },
});

const ID$4 = 'table-list-view-domain';
const viewConfig = (productId) => TableListViewConfig.decode({
  _id: ID$4,
  type: AppViewType.ListViewTable,
  base: 'devingen-dev',
  title: 'Domains',
  space: damgaSpace,
  table: dataConfigs['damga-domains'],
  query: {
    fields: [
      new DataTransferField('_created'),
      new DataTransferField('domain'),
      new DataTransferField('target'),
    ],
    filter: {
      comparison: Comparison.EqMongoOID,
      id: 'product._id',
      value: productId,
    },
    sort: [{ id: 'domain', order: 1 }]
  },
  ui: TableListViewConfig.decode({
    header: {
      actions: [{
          ui: {
            name: 'new-domain-and-target',
            buttonType: 'button',
            label: 'Add New'
          },
          data: {
            type: 'navigation',
            direction: 'forward',
            placement: ViewPlacement.Dialog,
            viewId: ID
          }
        }],
    },
    main: {
      actions: [
        {
          ui: {
            name: 'edit',
            label: 'Edit',
            buttonType: 'icon-button',
            iconName: 'uil uil-pen',
            view: 'flat'
          },
          data: {
            type: 'navigation',
            direction: 'forward',
            placement: ViewPlacement.Dialog,
            viewId: ID$1,
            params: {
              conversionConfig: {
                'item._id': 'domainId',
                'item.domain': 'domain',
                'item.target.value': 'target',
                'item.target.overrideHost': 'overrideHost',
                'item.target.useTLS': 'useTLS',
              },
            }
          }
        },
        {
          ui: {
            name: 'move',
            label: 'Move to another product',
            buttonType: 'icon-button',
            iconName: 'uil uil-exchange-alt',
            view: 'flat'
          },
          data: {
            type: 'navigation',
            direction: 'forward',
            placement: ViewPlacement.Dialog,
            viewId: ID$2,
            params: {
              conversionConfig: {
                'item._id': 'domainId',
              },
            }
          }
        },
        {
          ui: {
            name: 'delete',
            label: 'Delete domain',
            buttonType: 'icon-button',
            iconName: 'uil uil-trash',
            view: 'flat',
            tint: 'danger',
          },
          data: {
            type: 'navigation',
            direction: 'forward',
            placement: ViewPlacement.Dialog,
            viewId: ID$3,
            params: {
              conversionConfig: {
                'item._id': 'domainId',
              },
            }
          }
        },
      ],
      fields: [
        { fieldId: 'domain' },
        {
          fieldId: 'target.value',
          fieldDisplay: new NestedField('target.value', new TextField('value', 'Target')),
        },
        {
          fieldId: 'target.overrideHost',
          fieldDisplay: new NestedField('target.overrideHost', new BooleanField('overrideHost', 'Override Host')),
        },
        {
          fieldId: 'target.useTLS',
          fieldDisplay: new NestedField('target.useTLS', new BooleanField('useTLS', 'Use TLS')),
        },
        {
          fieldId: 'domain',
          fieldDisplay: new TextField('domain', 'DNS Record'),
          componentTag: 'devingen-product-damga-domain-table-list-dns-record',
        },
      ],
      columns: {
        'target.overrideHost': {
          width: 150,
        },
        'target.useTLS': {
          width: 150,
        },
      },
    },
  }),
});

export { ID$4 as I, ID as a, ID$1 as b, ID$2 as c, damgaSpace as d, ID$3 as e, config as f, config$1 as g, config$2 as h, config$3 as i, viewConfig as v };
